Re: Copying, hash, and isEqual:
Re: Copying, hash, and isEqual:
- Subject: Re: Copying, hash, and isEqual:
- From: Nicko van Someren <email@hidden>
- Date: Mon, 7 Jun 2004 08:57:19 +0100
On 7 Jun 2004, at 1:42, Marcel Weiher wrote:
On 7 Jun 2004, at 01:03, b.bum wrote:
OK. Fine... let me rephrase. "But the key is not the same!"
I hope you don't mean this seriously? Are you trying to tell me that
an object is not "the same" as *itself*???
No, he's saying that an object is not the same as a copy of itself
unless there is some way (e.g. -hash and -isEqual:) to tell if they are
the same and that dictionary keys are copies (which is documented).
The bottom line is this:
- NS*Dictionary copies keys. It is documented as doing so and this
is the intended behavior, by design.
YES. That is why I have said numerous times that this is a *design*
bug. Goodness.
No, it's not a design bug. Go back to the very start of this thread
and you'll find that the original poster complained about the
incompleteness (shallowness) of the copying of the keys if they are
containers. If the keys are mutable then it is important that they get
copied since the original might get changed after it's inserted as a
key but the hash of the key is only taken at the time of insertion.
- if object (A) and object (B) are to be used as a key in a
dictionary, then -hash and -isEqual: need to be overridden
accordingly.
Which isn't documented. In fact, the documentation for -hash
recommends the *opposite*. But that's a very minor issue...
The documentation for hash says that it must return the same value for
two objects if -isEqual: returns YES for that pair. The documentation
for -isEqual: says that it's up to your class as to what is "equal" and
you need to define it if you add instance variables. NSDictionary
states that it uses -hash and -isEqual: so it should be fairly obvious
that these need to be properly implemented in a class if your going to
use instances of that class as keys. If copies of objects are to be
deemed equivalent that you need to implement these messages.
Your implementation does not follow the second rule. Your key is
not the *very same identical* key as you claim.
Of course it is!
The copy of the object is not the same as the object. The base
implementation of NSObject uses the object address in the -hash and
-isEqual: implementations but it also does not implement -copyWithZone:
so there is no way to get a copy of an existing NSObject (or subclass),
so you can't assume that the base implementations are useful for copies
of your subclass. If you implement copying then you need to make a
discussion as to the equivalence of the original and the result of
copying.
@interface MyObject : NSObject <NSCopying>
{}
@end
@implementation MyObject
-copyWithZone:(NSZone*)zone
{
id copy=[[[self class] alloc] init];
return copy;
}
@end
Incidentally, given your object was immutable (implicitly, since is had
no instance variables) it would have been legitimate to simply return
self in this -copyWithZone: (which is what happens with immutable
strings).
Nicko
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
References: | |
| >Ugly bug in Foundation, beware! (From: Ondra Cada <email@hidden>) |
| >Is that really a bug at all? (was: Re: Ugly bug in Foundation, beware!) (From: Alastair Houghton <email@hidden>) |
| >Re: Is that really a bug at all? (was: Re: Ugly bug in Foundation, beware!) (From: Ondra Cada <email@hidden>) |
| >Re: Is that really a bug at all? (was: Re: Ugly bug in Foundation, beware!) (From: Alastair Houghton <email@hidden>) |
| >Re: Is that really a bug at all? (was: Re: Ugly bug in Foundation, beware!) (From: Brent Gulanowski <email@hidden>) |
| >Re: Is that really a bug at all? (was: Re: Ugly bug in Foundation, beware!) (From: Alastair Houghton <email@hidden>) |
| >Re: Is that really a bug at all? (was: Re: Ugly bug in Foundation, beware!) (From: Ondra Cada <email@hidden>) |
| >Re: Is that really a bug at all? (was: Re: Ugly bug in Foundation, beware!) (From: Marcel Weiher <email@hidden>) |
| >Re: Is that really a bug at all? (was: Re: Ugly bug in Foundation, beware!) (From: Brent Gulanowski <email@hidden>) |
| >Re: Is that really a bug at all? (was: Re: Ugly bug in Foundation, beware!) (From: "Louis C. Sacha" <email@hidden>) |
| >Re: Is that really a bug at all? (was: Re: Ugly bug in Foundation, beware!) (From: Marcel Weiher <email@hidden>) |
| >Re: Is that really a bug at all? (was: Re: Ugly bug in Foundation, beware!) (From: Nat! <email@hidden>) |
| >Re: NSDictionary design bug (was: Re: Ugly bug in Foundation, beware!) (From: Marcel Weiher <email@hidden>) |
| >Copying, hash, and isEqual: (From: "b.bum" <email@hidden>) |
| >Re: Copying, hash, and isEqual: (From: Marcel Weiher <email@hidden>) |
| >Re: Copying, hash, and isEqual: (From: "b.bum" <email@hidden>) |
| >Re: Copying, hash, and isEqual: (From: Marcel Weiher <email@hidden>) |