RE: When do I need to override hash?
RE: When do I need to override hash?
- Subject: RE: When do I need to override hash?
- From: Ben Trumbull <email@hidden>
- Date: Thu, 20 Aug 2009 20:53:01 -0700
The -hash method is important for objects that are used as keys in
associative collections.
[snip]
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) ...
Is there any way that you can tell that some higher-level technology
you are using (CoreData?) is putting your objects into an NSSet?
That's presumably a hidden implementation detail which you can't
assume one way or the other with any safety?
Core Data plays by the same rules as everyone else regarding -hash, -
isEqual and Cocoa collection classes. Every pair of objects that
return YES from -isEqual must return the same -hash result.
Core Data doesn't use random objects as keys in dictionaries or sets
for this reason. It's not that we don't trust you, but ... to
prevent misunderstandings, all NSManagedObject subclasses are
forbidden from overriding -hash and -isEqual. Since NSManagedObjects
have very specific semantic meanings associated with -isEqual and
[[self objectID] isEqual:] no good could come of it anyway.
If you need to know whether or not another object has put your object
into an NSDictionary, you're probably doing something wrong. Do you
have a specific concern about Core Data using your objects ?
- Ben
_______________________________________________
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