Re: Copying, hash, and isEqual:
Re: Copying, hash, and isEqual:
- Subject: Re: Copying, hash, and isEqual:
- From: Marcel Weiher <email@hidden>
- Date: Mon, 7 Jun 2004 00:43:03 +0100
On 6 Jun 2004, at 18:03, b.bum wrote:
Subject changed -- this is not to comment upon NSDictionary's
documented behavior of copying its keys. If that doesn't suit your
needs, create a CFDictionary with the appropriate behavior and cast it
to NSDictionary.
Which isn't portable. In that case, I'd rather implement my own.
This message focuses specifically upon -hash, -isEqual: and copying.
If there is one "invariant" a dictionary should obey, it is that the
following must hold:
[dict setObject:b forKey:a];
b == [dict objectForKey:a];
[my code snipped]
id pool=[NSAutoreleasePool new];
id a=[MyObject new];
id result=@"Hello world!";
id dict=[NSMutableDictionary dictionary];
[dict setObject:result forKey:a];
result = [dict objectForKey:a];
But your keys are not the same!
You are using the plural "keys". I am only using a *single* key. I am
storing something and retrievig it using not just a key that is somehow
equivalent, but the *very same identical* key. Yet NSDictionary can't
find the value. That's a bug! The fact that NSDictionary turns this
into two keys is the bug.
[Discussion of facts I am fully aware of snipped]
Your definition of MyClass has no internal state, therefore you could
make the above work by adding something like the following:
Of course I could. However, you are asking my NSObject subclass, which
is obeying all the rules for implementations of -hash, -isEqual: and
NCopying to work around a bug in NSDictionary!
[snip]
Given that NSObject's implementaton of -hash and -isEqual: cannot
possibly know anything about your subclass's internal state, the
default implementation is simplistic, but highly predictable.
Precisely. And in fact, the -hash doc. recommends something like this
(avoid internal state), yet NSDictionary breaks if you actually follow
the advice given.
Cheers,
Marcel
--
Marcel Weiher Metaobject Software Technologies
email@hidden www.metaobject.com
Metaprogramming for the Graphic Arts. HOM, IDEAs, MetaAd etc.
1d480c25f397c4786386135f8e8938e4
_______________________________________________
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>) |