I have a class named Person
and created a Person instance
"person".
Person *person = [Person personWithName:@"Kyle", andAge:15];
Then I tried to encode it using method archivedDataWithRootObject:requiringSecureCoding:error:
.
NSData *personData = [NSKeyedArchiver archivedDataWithRootObject:person
requiringSecureCoding:YES error:nil];
However, the personData
always returns nil
. Did I miss something?
Person.h
@interface Person : NSObject<NSSecureCoding>
@property (strong, nonatomic) NSString *name;
@property (assign, nonatomic) NSInteger age;
+ (instancetype)personWithName:(NSString *)name andAge:(NSInteger)age;
@end
Person.m
@implementation Person
+ (instancetype)personWithName:(NSString *)name andAge:(NSInteger)age{
Person *p = [Person new];
p.name = name;
p.age = age;
return p;
}
+ (BOOL)supportsSecureCoding {
return YES;
}
- (id)initWithCoder:(NSCoder *)coder {
self = [super initWithCoder:coder]; // error: No visible @interface for 'NSObject' declares the selector 'initWithCoder'
return self;
}
@end
Update (after implementing +supportsSecureCoding in .m):
Class 'Person' has a superclass that supports secure coding, but 'Person' overrides -initWithCoder: and does not override +supportsSecureCoding. The class must implement +supportsSecureCoding and return YES to verify that its implementation of -initWithCoder: is secure coding compliant.