• 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: When do I need to override hash?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: When do I need to override hash?


  • Subject: Re: When do I need to override hash?
  • From: Alastair Houghton <email@hidden>
  • Date: Thu, 20 Aug 2009 22:31:11 +0100

On 20 Aug 2009, at 22:16, Clark Cox wrote:

On Thu, Aug 20, 2009 at 2:14 PM, Kyle Sluder<email@hidden> wrote:
On Thu, Aug 20, 2009 at 2:01 PM, Clark Cox<email@hidden> wrote:
Yes, and two different objects will have different pointer values. If
the hash is based on the pointer values, then two different objects
cannot have the same hash, regardless of whether or not they are
equal. Hence, that implementation of hash is broken for any object
that does anything other than a pointer comparison in -isEqual:.

I thought we were talking about -hash just returning zero?

--Kyle Sluder


No, we were talking about:

OK, so this discussion is getting a little crazy :-)

The -hash method is important for objects that are used as keys in associative collections. So the worry about the hash value changing when an object's properties are altered is a bit of a red herring, because you *aren't supposed to change keys in associative collections*. If you do that in just about *any* implementation, ObjC or otherwise, you'll get undefined behaviour.

So, in practice, it's perfectly safe in 99.9% of cases to base your hash off your object's properties. In the specific case when you're mutating objects that are keys in associative collections (NSDictionary and NSSet being the primary examples, along with their CoreFoundation counterparts), if you change a property that affects *either* -isEqualTo: *or* -hash, you need to remove the object before mutating it and then add it back again afterwards.

The one other thing I'll note is that occasionally you might be using an object as a key where only *some* of its properties contribute towards -hash and -isEqualTo:. In that case, you can safely change any of the other non-contributing properties.

Kind regards,

Alastair.

--
http://alastairs-place.net



_______________________________________________

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


  • Follow-Ups:
    • Re: When do I need to override hash?
      • From: Seth Willits <email@hidden>
    • RE: When do I need to override hash?
      • From: Jeff Laing <email@hidden>
References: 
 >When do I need to override hash? (From: Gideon King <email@hidden>)
 >Re: When do I need to override hash? (From: Seth Willits <email@hidden>)
 >Re: When do I need to override hash? (From: David Duncan <email@hidden>)
 >Re: When do I need to override hash? (From: Clark Cox <email@hidden>)
 >Re: When do I need to override hash? (From: Bryan Henry <email@hidden>)
 >Re: When do I need to override hash? (From: Clark Cox <email@hidden>)
 >Re: When do I need to override hash? (From: Kyle Sluder <email@hidden>)
 >Re: When do I need to override hash? (From: Shawn Erickson <email@hidden>)
 >Re: When do I need to override hash? (From: Clark Cox <email@hidden>)
 >Re: When do I need to override hash? (From: Kyle Sluder <email@hidden>)
 >Re: When do I need to override hash? (From: Clark Cox <email@hidden>)

  • Prev by Date: Field editor for custom tableview cells contains wrong string
  • Next by Date: Re: How to disable the table view when search bar is active
  • Previous by thread: Re: When do I need to override hash?
  • Next by thread: RE: When do I need to override hash?
  • Index(es):
    • Date
    • Thread