views:

249

answers:

4

I have a sample database file called albums.plist It is structured as below (very simple layout).

I am new to Objective-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"&gt;
<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];
A: 

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 ];
}
codelogic
A: 

[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);
Ole Begemann
One last question - say I wanted to show via NSLog the total number of dictionaries in the array - is there an easy way to do this?
Steven McDonald
Its ok I worked that one out!In case it helps anyone else:NSLog(@"There are %d records in the file.",[plistArray count]);
Steven McDonald
A: 

Thanks to you both. I like the format of the plist you mention above and could easily redo it like that to be more efficient. I should point out that the order of items in the plist does not itself need to ever be sorted a-z as such as the data I am going to be adding is already sorted A-Z and I don't have a need to add more entries through the program.

With all that in mind, could you show me a few lines of code to show the second album - David Bowie, Heroes via NSLog() when all the program has is the number of the entry ie. 2

Thanks again Ole and codelogic.

Steven McDonald
No matter if the data in the plist is sorted or not, the order of the key/value pairs in a dictionary is always undefined. I will update my answer to show you the code for the array solution.
Ole Begemann
Thank you very much! Your latest code is exactly what I was looking for! I have learnt so much from your help here. I am now building my app around this.
Steven McDonald
A: 

Thanks this helped me a lot!!!