Skip to content

Instantly share code, notes, and snippets.

@conradev
Created March 4, 2016 06:54
Show Gist options
  • Save conradev/f823d797b5f6d0fbb4d9 to your computer and use it in GitHub Desktop.
Save conradev/f823d797b5f6d0fbb4d9 to your computer and use it in GitHub Desktop.
Spot the error
@interface Person : NSObject
@property (nonatomic, readonly, copy) NSString *first;
@property (nonatomic, readonly, copy) NSString *last;
@property (nonatomic, readonly, copy) NSDate *birthdate;
@end
@implementation Person
- (instancetype)initWithFirst:(NSString *)first last:(NSString *)last birthdate:(NSDate *)birthdate {
self = [super init];
if (!self)
return nil;
_first = [first copy];
_last = [last copy];
_birthdate = [birthdate copy];
return self;
}
- (NSUInteger)hash {
return (_first.hash ^ _last.hash ^ _birthdate.hash);
}
- (BOOL)isEqual:(Person *)object {
if (self == object)
return YES;
if (![object isKindOfClass:[self class]])
return NO;
return ([_first isEqual:object.first],
[_last isEqual:object.last] &&
[_birthdate isEqual:object.birthdate]);
}
@end
NSDate *date = [NSDate date];
Person *cosmo = [[Person alloc] initWithFirst:@"Cosmo" last:@"Kramer" birthdate:date];
Person *conrad = [[Person alloc] initWithFirst:@"Conrad" last:@"Kramer" birthdate:date];
BOOL equal = [cosmo isEqual:conrad];
NSLog(@"%@ and %@ are %@equal!", cosmo.first, conrad.first, (equal ? @"" : @"not "));
Cosmo and Conrad are equal!
@aciidgh
Copy link

aciidgh commented Mar 4, 2016

[_first isEqual:object.first], => [_first isEqual:object.first] &&

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment