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 01:42:49 +0100
On 7 Jun 2004, at 01:03, b.bum wrote:
On Jun 6, 2004, at 4:43 PM, Marcel Weiher wrote:
On 6 Jun 2004, at 18:03, b.bum wrote:
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.
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*???
I have to admit that is one of the weirdest claims I have seen made in
a long time, apart from simply being not true. The fact that you are
making such absurd claims to defend NSDictionary's broken
copying-behaviour should be a clue that something is very, very wrong.
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.
- 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...
Your implementation does not follow the second rule. Your key is not
the *very same identical* key as you claim.
Of course it is!
------------- dictbug.m --------------
marcel@tuvuk[tmp]cat dictbug.m
#import <Foundation/Foundation.h>
@interface MyObject : NSObject <NSCopying>
{}
@end
@implementation MyObject
-copyWithZone:(NSZone*)zone
{
id copy=[[[self class] alloc] init];
return copy;
}
@end
int main(int argc, char *argv[] )
{
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];
NSLog(@"%@",result);
NSLog(@"a==a: %d",a==a);
NSLog(@"[a isEqual:a]: %d",[a isEqual:a]);
NSLog(@"[a hash]==[a hash] %d",[a hash]==[a hash]);
[pool release];
return 0;
}
marcel@tuvuk[tmp]./dictbug
2004-06-07 01:40:50.290 dictbug[29128] (null)
2004-06-07 01:40:50.290 dictbug[29128] a==a: 1
2004-06-07 01:40:50.291 dictbug[29128] [a isEqual:a]: 1
2004-06-07 01:40:50.291 dictbug[29128] [a hash]==[a hash] 1
NS*Dictionary is working exactly as documented and as designed.
Exactly. That is why it is a *design* bug, as I have said from the
beginning.
Clear now?
Cheers,
Marcel
_______________________________________________
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>) |