Skip to content

Instantly share code, notes, and snippets.

@chriseidhof
Created Dec 16, 2012
Embed
What would you like to do?
@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