I have an NSObject subclass in which I implemented the isEqual: and hash methods as follows for testing purposes:
- (BOOL)isEqual:(id)anObject {
return YES;
}
- (NSUInteger)hash {
return 1;
}
For some reason, I can add multiple objects of this class to an NSMutableSet even though they are the “same”. Is there any reason this would not be working?
Update: It turns out I was assigning an NSMutableArray instance to an ivar of type NSMutableSet. D’oh!
There must be something else in your code that you are not doing right, because overriding methods as you show in the OP causes
NSMutableSetto recognize my objects as identical:Test.h:
Test.m:
main.c:
This snippet produces
1, as expected.