I have an NSMutableSet set that contains custom made objects who are a subclass of SKNode. I am making a game where these objects are added and removed from the NSMutableSet. I am adding and removing from the main thread, so threading isn't an issue. For some reason sometimes an object isn't removed because it can't be found. The following method returns NO:
[self.set containsObject:object]
I looked into the this problem and printed the address and hash number of the object and all the objects in the NSMutableSet, and sure enough it appears in the set.
What could be the reason that the object isn't found if the address and hash numbers equal? I understand that the containsObject method uses the isEqual which compares these two values.
The result of
hashmust be equal and the result ofisEqual:must beYES. Just matching the hash is not sufficient. Have you checkedisEqual:? The defaultisEqual:compares object identity, nothash.hashis allowed to be used by collections to speed up comparisons, but it is only an optimization. If two objects returnYESforisEqual:they must also return the samehash, but the converse is not true.