Whenever you implement a custom class and you implement the isEqual:
method, you must also implement the hash
method. See the docs for NSObject
for details.
If these two methods do not properly work together then using the object in any sort of dictionary or set (basically any sort of hash map), then things won't work properly.
Note that two objects that return YES
for isEqual:
must return the same value for hash
(which is not the default). However, two objects that are not equal (based on isEqual:
) may or may not have the same hash
value.
In theory you could simply return 1
for the hash
of every object and things would work but it would make dictionaries and sets of these objects work very inefficiently. So make some attempt for unequal objects return different hash
values.