Skip to content

Instantly share code, notes, and snippets.

@x0000ff
Last active March 27, 2017 20:59
Show Gist options
  • Save x0000ff/f099a622e979ed8e435fd55ce3744dc8 to your computer and use it in GitHub Desktop.
Save x0000ff/f099a622e979ed8e435fd55ce3744dc8 to your computer and use it in GitHub Desktop.
Implementation of `isEqual:` http://nshipster.com/equality/
@interface UserModel : NSObject
@property (copy, nonatomic) NSString *name;
@property (copy, nonatomic) NSNumber *age;
@end
@implementation UserModel
/********************************
* NSHipster: Equality
* http://nshipster.com/equality/
********************************/
- (BOOL)isEqual:(id)otherId
{
if (!otherId) {
return NO;
}
if (otherId == self) {
return YES;
}
if (![otherId isKindOfClass:self.class]) {
return NO;
}
UserModel *other = (UserModel *)otherId;
BOOL namesAreEqual = (!self.name && !other.name) || [self.name isEqualToString:other.name];
if (!namesAreEqual) { return NO; }
BOOL agesAreEqual = (!self.age && !other.age) || [self.age isEqualToNumber:other.age];
if (!agesAreEqual) { return NO; }
return YES;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment