Re: NSDictionary design bug (was: Re: Ugly bug in Foundation, beware!)
Re: NSDictionary design bug (was: Re: Ugly bug in Foundation, beware!)
- Subject: Re: NSDictionary design bug (was: Re: Ugly bug in Foundation, beware!)
- From: Marcel Weiher <email@hidden>
- Date: Sun, 6 Jun 2004 17:00:27 +0200
As the copy will satisfy the -hash and -isEqual "identity", the copy
is just as good as the original for the purpose.
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];
NSDictionary does not obey this invariant. I have created a little
test program to make this more clear. We create an NSObject subclass
that faithfully implements the NSCopying protocol as documented. We
can use objects of this class as keys to our dictionary, but we can
never get the objects back out again, even using the exact same key!!
It also faithfully implements the recommendation for "hash", and yet it
breaks...
------------------------ 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);
[pool release];
return 0;
}
_______________________________________________
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>) |