Seth Willits wrote:
On Aug 19, 2009, at 4:28 PM, Gideon King wrote:

So do I need to override hash too? If so, are there any recommendations as to how to determine the hash easily?


I probably shouldn't admit this, but I've yet to override hash and have yet to notice any problems. The docs say I should, so I realize I likely should, but I would really like a concrete reason to do so. I've never called hash, nor seen it ever called by anyone else's code. I assume the frameworks call it from somewhere, but where?


When it sticks things in any kind of hashtable or wants for some reason a way to partition objects roughly to cut down the number of comparisons it has to do on them. So it's very important that if two objects are isEqual: to each other, they must have the same hash. Else what could happen is you insert one into any of the collections which use hash to bucket the objects, then you put another one in which isEqual: to the first. If it has a different hash (as it's not supposed to do), the code will go look in the wrong bucket, won't find the object you put there earlier and then you'll have two objects in the collection which are isEqual: to each other.
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to