Skip to content

Instantly share code, notes, and snippets.

@onurhuseyincantay
Last active January 16, 2019 14:17
Show Gist options
  • Save onurhuseyincantay/faf5a2da104b1b6731935b9d19166215 to your computer and use it in GitHub Desktop.
Save onurhuseyincantay/faf5a2da104b1b6731935b9d19166215 to your computer and use it in GitHub Desktop.
Equality in objective-c
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