Last active
March 27, 2017 20:59
-
-
Save x0000ff/f099a622e979ed8e435fd55ce3744dc8 to your computer and use it in GitHub Desktop.
Implementation of `isEqual:` http://nshipster.com/equality/
This file contains hidden or 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 UserModel : NSObject | |
@property (copy, nonatomic) NSString *name; | |
@property (copy, nonatomic) NSNumber *age; | |
@end |
This file contains hidden or 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
@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