On Sep 18, 2008, at 7:12 PM, Keith Duncan wrote:

if [objectA isEqual:objectB] returns true then [objectA hash] == [objectB hash] must be true


the object's hash cannot be derived from any mutable properties of the object

So, with both those points in mine how exactly does one implement it.

I think that for all practical purposes, the answer is that you just aren't allowed to mutate objects that are in collections. The documentation for -hash gives the example that "a mutable dictionary can be put in a hash table but you must not change it while it is in there".

One counterexample is internal storage that isn't integral to the object's value. For example, an object may cache a value in some internal storage while it's in a collection, but that cached value can't participate in either -isEqual: or -hash. That is, the cached value is incidental and doesn't change the object's externally visible value.

Another counterexample is an object which doesn't provide value semantics. That is, its -isEqual: only tests identify (pointer comparison) and whose hash is purely based on the object pointer. Like the default NSObject implementation. Such an object never compares equal to any other object. It may also be safely mutated within a collection. (However, such an object would be a poor candidate for a dictionary key, for example. Dictionary keys are copied, and since the copy can never equal the original, you'd never be able to look up the value using the original key. It'd be fine in a set, though.)

Cheers,
Ken

_______________________________________________

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 [EMAIL PROTECTED]

Reply via email to