-
-
Save chriseidhof/78b3ce0edbcdf0d202e2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface Person : NSObject <NSCoding> | |
- (id)initWithName:(NSString*)name birthDate:(NSString*)birthDate; | |
@property (nonatomic, copy, readonly) NSString* name; | |
@property (nonatomic, strong, readonly) NSDate* birthDate; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "Person.h" | |
static NSString* const kName = @"name"; | |
static NSString* const kDate = @"date"; | |
@implementation Person | |
- (id)initWithName:(NSString*)name birthDate:(NSString*)birthDate { | |
self = [super init]; | |
if(self) { | |
_name = [name copy]; | |
_birthDate = birthDate; | |
} | |
return self; | |
} | |
- (id)initWithCoder:(NSCoder*)aDecoder { | |
self = [super init]; | |
if (self) { | |
_name = [aDecoder decodeObjectForKey:kName]; | |
_date = [aDecoder decodeObjectForKey:kDate]; | |
} | |
return self; | |
} | |
- (BOOL)isEqual:(id)obj { | |
if(![obj isKindOfClass:[Person class]]) return NO; | |
Person* other = (Person*)obj; | |
BOOL nameIsEqual = _name == _other->_name || [_name isEqual:other->_name]; | |
BOOL dateIsEqual = _date == _other->_date || [_date isEqual:other->_date]; | |
return nameIsEqual && dateIsEqual; | |
} | |
- (void)encodeWithCoder:(NSCoder*)aCoder { | |
[aCoder encodeObject:_name forKey:kName]; | |
[aCoder encodeObject:_date forKey:kDate]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment