Last active
January 16, 2019 14:17
-
-
Save onurhuseyincantay/faf5a2da104b1b6731935b9d19166215 to your computer and use it in GitHub Desktop.
Equality in objective-c
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
NSString *compareableString = @“This is a String”; | |
NSString *comparedString = @“This is a String”; | |
BOOL *isEqual = (*comparableString == *comparedString); | |
// this will return NO because it refers to two different objects | |
/* | |
So we have to use equality methods from their objects to compare them. | |
If we want to use our custom equality functions | |
we also should be sure that the function checks for the pointer equality as a first step of equality function.*/ | |
if(self == object) return YES; | |
//we also should be sure to have a hash function to check the second equality step. | |
-(NSUInteger)hash{ | |
NSUInteger *firstNameHash = [_firstName hash]; | |
NSUInteger *lastNameHash = [_firstName hash]; | |
NSUInteger *ageHash = [_firstName hash]; | |
return firstNameHash ^ lastNameHash ^ ageHash; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment