The documentation for -hash
says it must not change while a mutable object is stored in a collection, and similarly the documentation for -isEqual:
says the -hash
value must be the same for equal objects.
Given this, does anybody have any suggestions for the best way to implement -hash
such that it meets both these conditions and yet is actually calculated intelligently (i.e. doesn't just return 0
)? Does anybody know how the mutable versions of framework-provided classes do this?
The simplest thing to do is of course just forget the first condition (about it not changing) and just make sure I never accidentally mutate an object while it's in a collection, but I'm wondering if there's any solution that's more flexible.
EDIT: I'm wondering here whether it's possible to maintain the 2 contracts (where equal objects have equal hashes, and hashes don't change while the object is in a collection) when I'm mutating the internal state of the object. My inclination is to say "no", unless I do something stupid like always return 0 for the hash, but that's why I'm asking this question.