Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @interface Person : NSObject
- {
- }
- @property (nonatomic,copy) NSString *firstName;
- @property (nonatomic,copy) NSString *lastName;
- @end
- @implementation Person
- - (instancetype)initWithCoder:(NSCoder *)coder
- {
- self = [super init];
- self.firstName = [coder decodeObjectForKey:@"firstName"];
- self.lastName = [coder decodeObjectForKey:@"lastName"];
- return self;
- }
- - (void)encodeWithCoder:(NSCoder *)coder
- {
- [coder encodeObject:self.firstName forKey:@"firstName"];
- [coder encodeObject:self.lastName forKey:@"lastName"];
- }
- @end
- // create a person
- Person *person = [[Person alloc] init];
- person.firstName = @"John";
- person.lastName = @"Doe";
- NSData *personData = [NSKeyedArchiver archivedDataWithRootObject:person];
- @protocol NSCoding
- - (void)encodeWithCoder:(NSCoder *)aCoder;
- - (nullable instancetype)initWithCoder:(NSCoder *)aDecoder; // NS_DESIGNATED_INITIALIZER
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement