Advertisement
Guest User

Untitled

a guest
Jun 29th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. @interface Person : NSObject
  2. {
  3.  
  4. }
  5.  
  6. @property (nonatomic,copy) NSString *firstName;
  7. @property (nonatomic,copy) NSString *lastName;
  8.  
  9. @end
  10.  
  11. @implementation Person
  12.  
  13. - (instancetype)initWithCoder:(NSCoder *)coder
  14. {
  15. self = [super init];
  16.  
  17. self.firstName = [coder decodeObjectForKey:@"firstName"];
  18. self.lastName = [coder decodeObjectForKey:@"lastName"];
  19.  
  20. return self;
  21.  
  22. }
  23.  
  24. - (void)encodeWithCoder:(NSCoder *)coder
  25. {
  26. [coder encodeObject:self.firstName forKey:@"firstName"];
  27. [coder encodeObject:self.lastName forKey:@"lastName"];
  28. }
  29.  
  30. @end
  31.  
  32. // create a person
  33. Person *person = [[Person alloc] init];
  34. person.firstName = @"John";
  35. person.lastName = @"Doe";
  36.  
  37. NSData *personData = [NSKeyedArchiver archivedDataWithRootObject:person];
  38.  
  39. @protocol NSCoding
  40.  
  41. - (void)encodeWithCoder:(NSCoder *)aCoder;
  42. - (nullable instancetype)initWithCoder:(NSCoder *)aDecoder; // NS_DESIGNATED_INITIALIZER
  43.  
  44. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement