I have a sample database file called albums.plist It is structured as below (very simple layout).
I am new to Ob开发者_高级运维jective-C programming and would learn a lot if someone is able to help me figure this out.
What I would like to do programatically is parse this database and for example, show on screen for example, the second albums details - that is album and artist - on two UILabels
One would be a UILabel called artist_name
and would therefore be set to: @"David Bowie"
and underneath another UILabel
called album_name
set to: @"Heroes"
Can someone help me out with this? I would learn a lot from it.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>The Beatles</key>
<string>Let It Be</string>
<key>David Bowie</key>
<string>Heroes</string>
<key>Dire Straits</key>
<string>Brothers In Arms</string>
</dict>
</plist>
I have came across code below which I could probably use to open access to the file.
I just need a few pointers as to how to access the second record in the database?
Many Thanks.
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"albums.plist"];
plistDictionary = [[NSDictionary dictionaryWithContentsOfFile:finalPath] retain];
If the dictionary loaded correctly, you should be able to access its keys and values using something like:
for ( id key in plistDictionary ) {
artist_name.text = key;
album_name.text = [ plistDictionary objectForKey:key ];
}
[plistDictionary allKeys]
will return an array of all the keys in the dictionary. You could then loop through the keys in the array or select one via its index. Be aware, however, that dictionaries have no defined order so you cannot rely on the order of the keys in the array. You could sort them, though.
NSArray *artists = [[plistDictionary allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
// Access the second album (after sorting)
NSString *secondAlbumName = [plistDictionary objectForKey:[artists objectAtIndex:1]];
NSLog(@"%@", secondAlbumName);
One suggestion: If you can change the format of your plist file, I would suggest you do so. Instead of a dictionary of artist name/album name pairs, consider an array of dictionaries. This would be a cleaner structure for your data IMHO and it would also simplify accessing the items in the array. Like this:
<array>
<dict>
<key>artist_name</key>
<string>The Beatles</string>
<key>album_name</key>
<string>Let It Be</string>
</dict>
<dict>
<key>artist_name</key>
<string>David Bowie</string>
<key>album_name</key>
<string>Heroes</string>
</dict>
</array>
The code to read such a file would be something like this:
NSArray* plistArray = [NSArray arrayWithContentsOfFile:finalPath];
// Access the second entry in the array:
NSDictionary *album = [plistArray objectAtIndex:1];
NSString *artistName = [album objectForKey:@"artist_name"];
NSString *albumName = [album objectForKey:@"album_name"];
NSLog(@"%@ - %@", artistName, albumName);
精彩评论