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: Mon, 7 Jun 2004 00:52:58 +0100
------------------------ 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
When you copy the key here, you create a new object. *BUT* NSObject,
and
therefore MyObject implements isEqual: as '==' so the copied key is not
equal anymore to the one you passed to the dictionary... Hence, you can
never retreive your object from the dictionary.
Yes, precisely. I am not sure why you (and everyone else??) think you
have to explain this to me, because the sample code was constructed to
exploit/show this. I thought this was obvious, but obviously not
obvious enough.
You should either make a clever isEqual: implementation for MyObject,
or
heed the recomendation from the documentation:
No, I should not have to work around NSDictionary's buggy
implementation. Furthermore, what I consider -isEqual: is
application-dependent.
Implement NSCopying by retaining the original instead of creating
a new
copy when the class and its contents are immutable.
Who says the contents are immutable (just add an instance var...)
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.