• 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
[NSMutableDictionary setObject:forKey:] problem
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[NSMutableDictionary setObject:forKey:] problem


  • Subject: [NSMutableDictionary setObject:forKey:] problem
  • From: Joachim <email@hidden>
  • Date: Wed, 19 Oct 2005 14:49:40 -0700

First off, thank you to all you talented developers who answer all the questions on this list. As a newbie, it has helped me tremendiously! This time, however, I'm stuck. Here it goes...

I'm building a class that's supposed to know everything my code needs to know about iPhoto and the albums herein. One step is to compose an NSMutableDictionary ivar with an entry for each album (key = album name, value = dictionary with album id, album type and photo count). I know the path to AlbumData.xml and parses the file with the code below.

The problem is at the setObject:forKey: call. The object is not added to the NSMutableDictionary. No errors or anything, just zero elements.

- (void) parseXMLData
{
// Parses the XML data file and initializes the albumDict ivar (NSMutableDictionary *)
// albumDataXMLFilePath contains the path to the AlbumData.xml file


NSDictionary *iPhotoDict = [[NSDictionary alloc] initWithContentsOfFile:albumDataXMLFilePath];
NSArray *iPhotoAlbums = [iPhotoDict objectForKey:@"List of Albums"];
NSEnumerator *enumerator = [iPhotoAlbums objectEnumerator];
id iPhotoAlbumDict;


    // Initialise albumDict ivar
    if (albumDict)
        [albumDict release];
    [albumDict initWithCapacity:[iPhotoAlbums count]];

// Step through iPhoto albums (Library, smart albums, slideshows, books etc.)
while (iPhotoAlbumDict = [enumerator nextObject]) {


if (true || [iPhotoAlbumDict respondsToSelector:@selector (objectForKey)]) {
// The second half of the above expression always returns false,
// even though iPhotoAlbumDict DOES respond to the message below
[albumDict
setObject:[[NSDictionary alloc] initWithObjectsAndKeys:
[[iPhotoAlbumDict objectForKey:@"AlbumId"] copy], @"AlbumId",
[[iPhotoAlbumDict objectForKey:@"Album Type"] copy], @"AlbumType",
[[iPhotoAlbumDict objectForKey:@"PhotoCount"] copy], @"PhotoCount",
nil]
forKey:[iPhotoAlbumDict objectForKey:@"AlbumName"]];
// The object is NOT added to albumDict even though setObject: is
// supplied with a valid NSDictionary (I split the code up to make
// sure [[NSDictionary alloc] initWith... did return what's expected)
}
}
[iPhotoDict release];
}


Here's my questions:

1) What's wrong with my code above? Why isn't the NSDictionary object added with the setObject:forKey: call?

2) Why does [iPhotoAlbumDict respondsToSelector:@selector (objectForKey)] always return false? ([iPhotoAlbumDict class] returns NSCFDictionary.)

3) Am I handling memory correctly in my code? No leaks etc? I'm still struggling to understand how Objective-C memory management works.

Finally, does NSDictionary read the whole file into memory in the initWithContentsOfFile: call or does it just scan the file later at the given entry levels I specify in my code? The AlbumData.xml file can get quite big, but I don't want to use NSXMLParser as it involves way too much code.

Thanks in advance,
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


  • Follow-Ups:
    • Re: [NSMutableDictionary setObject:forKey:] problem
      • From: m <email@hidden>
    • Re: [NSMutableDictionary setObject:forKey:] problem
      • From: Elias MÃ¥rtenson <email@hidden>
    • Re: [NSMutableDictionary setObject:forKey:] problem
      • From: j o a r <email@hidden>
  • Prev by Date: NSAutoreleasePool dilemma
  • Next by Date: Re: NSAutoreleasePool dilemma
  • Previous by thread: Re: NSAutoreleasePool dilemma
  • Next by thread: Re: [NSMutableDictionary setObject:forKey:] problem
  • Index(es):
    • Date
    • Thread