• 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: Nathan Vander Wilt <email@hidden>
  • Date: Wed, 19 Aug 2009 17:47:45 -0700

On Aug 19, 2009, at 4:28 PM, Gideon King wrote:
So do I need to override hash too? If so, are there any recommendations as to how to determine the hash easily?

If you need to override -isEqual: to provide something besides pointer comparison, you should also override -hash. If objects are equal, their hashes must be equal or you may trigger undefined behaviour in who knows what situations.


While this may not offer the best performance, the easiest way to implement -hash would be to take all the properties/internal state of your object that affect equality, format them into an NSString, and then return the hash of the string.

E.g.:

- (NSUInteger)hash {
NSString* valueRepresentation = [NSString stringWithFormat:@"%@ %lu",
self.companyName, self.departmentID];
return [valueRepresentation hash];
}


hope this helps,
-natevw
_______________________________________________

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: 
 >When do I need to override hash? (From: Gideon King <email@hidden>)

  • Prev by Date: Re: - [NSBitmapImageRep cacheDisplayInRect:toBitmapImageRep:] produces empty image
  • Next by Date: alias link to libcrypto not working across different OS X versions
  • 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