The documentation for -hash states that it should not change while the mutable object is stored in the collection, and similarly, the documentation for -isEqual: states that the -hash value must be the same for equal objects.
Given this, does anyone have any suggestions on the best way to implement -hash so that it matches these conditions, and yet it is actually calculated reasonably (i.e. does not return 0 )? Does anyone know how mutable versions of the classes provided by the infrastructure do this?
The easiest way to do this, of course, is simply to forget the first condition (without changing it) and just make sure that I have never accidentally mutated an object while it is in the collection, but I wonder if there is any solution that is more flexible.
EDIT: I am wondering if it is possible to maintain 2 contracts (where equal objects have equal hashes and hashes do not change while the object is in the collection) when I mutate the internal state of the object. My tendency is to say no if I am not doing something stupid, as always returning 0 for the hash, but that is why I ask this question.
objective-c cocoa-touch cocoa hash
Kevin Ballard Jan 14 '09 at 12:40 2009-01-14 12:40
source share