How to find an specific key/value (property list)
- by Bob Rivers
Hi,
I'm learning cocoa/objective-c. Right now I'm dealing with key/value coding. After reading Aaron's book and other sources, I thought that I was able to left the simple examples and try a complex one...
I'm trying read iTunes property list (iTunes Music Library.xml). I would like to retrieve the tracks held by an specific playlist.
Probably everybody knows it, but bellow I put a piece of the xml:
<plist version="1.0">
<dict>
<key>Major Version</key><integer>1</integer>
...
<key>Playlists</key>
<array>
<dict>
<key>Name</key><string>Library</string>
...
<key>Playlist Items</key>
<array>
<dict>
<key>Track ID</key><integer>10281</integer>
</dict>
...
</array>
</dict>
<dict>
...
</dict>
</array>
</dict>
</plist>
As you can see, the playlists are stored as dictionaries inside an array, and the key that identifies it is inside it, not as a <key> preceding it.
The problem is that I'm not able to figure out how to search for a key that is inside another one.
With the following code I can find the the array in which the playlists are stored, but how to find an specific <dict>?
NSDictionary *rootDict = [[NSDictionary alloc] initWithContentsOfFile:file];
NSArray *playlists = [rootDict objectForKey:@"Playlists"];
Here at Stackoverflow I found this post, but I'm not sure if iterate over the array and test it is a good idea.
I'm quite sure that I could use valueForKeyPath, but I'm unable to figure out how to do it.
Any help is welcome.
TIA,
Bob