Re: Weird behavior in NSMutableDictionary
Re: Weird behavior in NSMutableDictionary
- Subject: Re: Weird behavior in NSMutableDictionary
- From: Clark Cox <email@hidden>
- Date: Sat, 18 Dec 2004 12:06:56 -0500
On Fri, 17 Dec 2004 20:10:29 -0800 (PST), Harilaos Skiadas
<email@hidden> wrote:
> Hey guys,
>
> I just run into the following weird behavior, and
> probably I am missing something very obvious, but I
> just can't figure it out. I have an
> NSMutableDictionary *aDict, and an NSDictionary
> *anItem.
> What I want to do is add anItem as an object to aDict,
> with key the string that is the object corresponding
> the key @"aKey" in anItem. I would have thought that
> the following would work:
> [aDict setValue:anItem forKey:[anItem
> valueForKey:@"aKey"]];
> instead, I had to use:
> [aDict setValue:anItem forKey:[[anItem
> valueForKey:@"aKey"] copy];
>
> I thought that NSDictionary copies the keys when
> adding items,
No, the containers do not copy items added to them, they just retain
them. (Though the key is copied)
> so I don't quite see why I had to
> provide the copy.
What makes you think that you do?
> Is this a bug? Is it documented?
>From <http://developer.apple.com/documentation/Cocoa/Reference/Foundation/ObjC_classic/Classes/NSDictionary.html#//apple_ref/doc/uid/20000140/9552>
"Methods that add entries to dictionaries—whether as part of
initialization (for all dictionaries) or during modification (for
mutable dictionaries)—copy each key argument (keys must conform to the
NSCopying protocol) and add the copies to the dictionary. Each
corresponding value object receives a retain message to ensure that it
won't be deallocated before the dictionary is through with it."
--
Clark S. Cox III
email@hidden
http://www.livejournal.com/users/clarkcox3/
http://homepage.mac.com/clarkcox3/
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden