• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Implementing isEqual: and hash
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Implementing isEqual: and hash


  • Subject: Re: Implementing isEqual: and hash
  • From: Graham Cox <email@hidden>
  • Date: Sat, 23 Aug 2008 22:22:41 +1000


On 23 Aug 2008, at 10:13 pm, 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:.

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. If you compare only one thing
for equality, return it (if it's a primitive) or its hash value (if
you call isEqual: on it).



Thanks guys - works fine.


cheers, Graham _______________________________________________

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


References: 
 >Implementing isEqual: and hash (From: Graham Cox <email@hidden>)
 >Re: Implementing isEqual: and hash (From: "Michael Ash" <email@hidden>)

  • Prev by Date: Re: textfield not updating with bindings - Whats wrong?
  • Next by Date: Re: Implementing isEqual: and hash
  • Previous by thread: Re: Implementing isEqual: and hash
  • Next by thread: Re: Implementing isEqual: and hash
  • Index(es):
    • Date
    • Thread