Re: Implementing isEqual: and hash
Re: Implementing isEqual: and hash
- Subject: Re: Implementing isEqual: and hash
- From: "Adam R. Maxwell" <email@hidden>
- Date: Sat, 23 Aug 2008 09:38:52 -0700
On Aug 23, 2008, at 5:13 AM, Michael Ash wrote:
On Sat, Aug 23, 2008 at 7:41 AM, Graham Cox <email@hidden>
wrote:
I have a class for which equality can be defined as having the same
internal
string value (which happens to be a UUID-turned-string). I can easily
implement isEqual: based on that but the docs say I also need to
implement
-hash. Any pointers on what is a good way to do that? Could I just
safely
defer to the -hash returned by the string in question?
The implementation of -hash should *always* match the implementation
of -isEqual:.
And as Jim alluded, -hash must not depend on mutable properties; if
the hash changes while the object is in a hashing collection, you'll
end up with random crashes.
If you compare primitives in -isEqual:, you should combine them (using
xor or the like) in -hash. If you compare objects by calling -isEqual:
on them, you should combine their hashes (using xor or the like). If
you do some of each, combine them all.
What's the motivation for combining hashes in this case? I've
wondered what is the best thing to do when isEqual: is based on
comparing multiple ivars; I typically just use one of them for the hash.
thanks,
Adam
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
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