On Sep 18, 2008, at 4:39 PM, Jim Correia wrote:

On Sep 18, 2008, at 7:32 PM, Shawn Erickson wrote:

On Thu, Sep 18, 2008 at 4:19 PM, Keith Duncan <[EMAIL PROTECTED]> wrote:
if two objects compare equal, then they must have the same hash [...] [you] have to implement a corresponding -hash that maintains this invariant
rule.

Is there an example somewhere of what one should do to implement - hash to reflect -isEqual:, I haven't done so in one of my classes and it works fine. Would you just combine the -hash of the objects used to determine - isEqual:
in a logical manner, say XOR?

The only requirement is that if [objectA isEqual:objectB] returns true
then [objectA hash] == [objectB hash] must be true as well.

Additional care must be taken if you are implementing a mutable object and intend to store it in a collection: the object's -hash cannot change while it is in the collection (so effectively, the object's hash cannot be derived from any mutable properties of the object.)

This isn't a requirement of isEqual: or hash and in fact doing such a thing cannot break the requirement I stated and the obvious one I didn't state... that an object that shouldn't be considered equal to another needs to return false for [objectA isEqual:objectB] and hashes may or may not be that same value.

Expecting a set of mutable objects or a dictionary with mutable keys to work correctly (note NSDictionary copies keys for this very reason) under such situations in tenuous. It can be made to work if you own the code for the mutable objects being used and want to add such an assurance to your object's contract with out breaking the expectation of isEqual:.

-Shawn
_______________________________________________

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