So i'm overriding isEquals
and hash
to compare custom objects to be able to remove duplicates from a NSArray
. The problem is that i'm missing some values in the list which contains no duplicated items, and it seems that my hash
or isEquals
implementation is wrong. The custom object is a Course object which has some variables like: id
and name
I'll put the code here:
- (BOOL)isEqual:(id)object {
if ([object isKindOfClass:[Course self]]) {
return YES;
}
if(self == object){
return YES;
}
else {
return NO;
}
}
- (unsigned)hash {
NSString *idHash = [NSString stringWithFormat: @"%d", self._id];
return [idHash hash];
}
Then, after querying the database i put the values in an array and then in a set which should remove the duplicated items like this:
NSMutableSet *noDuplicates = [[NSMutableSet alloc] initWithArray:tempResults];
Can you see what i'm doing wrong in the isEquals
or hash
implementation?
Thanks a lot.