Re: When do I need to override hash?
Re: When do I need to override hash?
- Subject: Re: When do I need to override hash?
- From: Alastair Houghton <email@hidden>
- Date: Fri, 21 Aug 2009 09:25:35 +0100
On 21 Aug 2009, at 04:51, Seth Willits wrote:
On Aug 20, 2009, at 2:31 PM, Alastair Houghton wrote:
The -hash method is important for objects that are used as keys in
associative collections.
The documentation, nor did many others' comments on this topic, make
it clear that the mutability is only a problem for the *keys*.
Others and the docs talk about (paraphrasing) "putting an object
into a collection", not "using an object as a key in an associative
collection."
Here are the docs:
"If a mutable object is added to a collection that uses hash values
to determine the object’s position in the collection, the value
returned by the hash method of the object must not change while the
object is in the collection."
Indeed. This sentence could do with being made clearer IMO; my guess
is that it was written that way because people don't really think of
NSSet (or NSSet-like collections) as having keys per se, but it does
of course apply to objects held in an NSSet.
If it were only an issue for keys, then this is, like you said, no
big deal. If my reading of the documentation is correct, then it's a
much more prevalent problem, as others seem to be saying.
It *is* an issue for the keys (which includes objects that are held by
an NSSet or NSSet-like collection). That's why I said this particular
discussion was getting a bit crazy.
It is perfectly safe to mutate objects stored in an NSDictionary as
long as their keys don't change. The thing you mustn't change is the
key's "value", as determined by -isEqualTo: and -hash: (and, if
implemented, -compare:).
Kind regards,
Alastair.
--
http://alastairs-place.net
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden