• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: [NSMutableDictionary setObject:forKey:] problem
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [NSMutableDictionary setObject:forKey:] problem


  • Subject: Re: [NSMutableDictionary setObject:forKey:] problem
  • From: j o a r <email@hidden>
  • Date: Thu, 20 Oct 2005 07:44:55 +0200


On 20 okt 2005, at 04.15, Joachim wrote:

while (iPhotoAlbumDict = [enumerator nextObject]) {

A tip: It's common to write this type of assignment like so:

        while (nil != (iPhotoAlbumDict = [enumerator nextObject]))

To make it clear that you're not trying to compare two objects and forgot about using "==". There is even a GCC warning that will enforce you to use the notation above (double parentheses). You probably use -Wmost, which I think is default for Xcode, and it might not be included there.

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.

j o a r


Attachment: smime.p7s
Description: S/MIME cryptographic signature

 _______________________________________________
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

  • Follow-Ups:
    • Re: [NSMutableDictionary setObject:forKey:] problem
      • From: Joachim <email@hidden>
References: 
 >[NSMutableDictionary setObject:forKey:] problem (From: Joachim <email@hidden>)
 >Re: [NSMutableDictionary setObject:forKey:] problem (From: m <email@hidden>)
 >Re: [NSMutableDictionary setObject:forKey:] problem (From: Joachim <email@hidden>)

  • Prev by Date: Newbies: Completed Cocoa project available for download
  • Next by Date: Re: is ObjectAlloc broken?
  • Previous by thread: Re: [NSMutableDictionary setObject:forKey:] problem
  • Next by thread: Re: [NSMutableDictionary setObject:forKey:] problem
  • Index(es):
    • Date
    • Thread