Skip to content

Instantly share code, notes, and snippets.

@chriseidhof
Created December 16, 2012 15:15
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chriseidhof/78b3ce0edbcdf0d202e2 to your computer and use it in GitHub Desktop.
Save chriseidhof/78b3ce0edbcdf0d202e2 to your computer and use it in GitHub Desktop.
@interface Person : NSObject <NSCoding>
- (id)initWithName:(NSString*)name birthDate:(NSString*)birthDate;
@property (nonatomic, copy, readonly) NSString* name;
@property (nonatomic, strong, readonly) NSDate* birthDate;
@end
#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