Re: [NSMutableDictionary setObject:forKey:] problem
Re: [NSMutableDictionary setObject:forKey:] problem
- Subject: Re: [NSMutableDictionary setObject:forKey:] problem
- From: Joachim <email@hidden>
- Date: Fri, 21 Oct 2005 00:41:03 -0700
On 19-Oct-05, at 10:44 PM, j o a r wrote:
if ([iPhotoAlbumDict respondsToSelector:@selector
(objectForKey:)]) {
[albumDict
setObject:[NSDictionary dictionaryWithObjectsAndKeys:
[[iPhotoAlbumDict objectForKey:@"AlbumId"]
copy], @"AlbumId",
[[iPhotoAlbumDict objectForKey:@"Album Type"]
copy], @"AlbumType",
[[iPhotoAlbumDict objectForKey:@"PhotoCount"]
copy], @"PhotoCount",
nil]
forKey:[iPhotoAlbumDict
objectForKey:@"AlbumName"]];
There are additional leaks here that I missed earlier. Note that
you're copying the values that you add to the dictionary. Copying
here would probably be done be done for two reasons:
1) You want to ensure that the instance you hold is truly your own,
and not a mutable instance that someone else can change from under
your feet.
2) To increment the retain count of an immutable object.
If you're worried about #1, which I don't think you should have to
be for a dictionary that you yourself have restored from a XML
file, you need to do this:
[[[d objectForKey: k] copy] autorelease]
If you did it because of #2, please note that it's not necessary
(and in fact wrong -> leak), because these objects will be retained
by the dictionary when added to the dictionary. All Cocoa
collections works that way. That's also why you changed your code
from "initWithObjectsAndKeys:" to "dictionaryWithObjectsAndKeys:"
in your latest code revision.
Thank you very much for clarifying that. It's all making more and
more sense now. I've now removed the copy messages.
Joachim
_______________________________________________
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