View Full Version : Reading plist into array of dictionaries help

May 20, 2011, 10:12 PM
Hi all,

I am very new to app programming. I am trying to read a plist file into my app but can't figure out how to create the array in the first place and then read in the file. The plist will contain a varying number of dictionaries each consisting of 3 strings. I tried the code

NSDictionary productList[];

based on an array of intergers I saw on another forum but Xcode says "an array of interface 'NSDictionary' is invalid". How do I create this array? Sorry to ask something that I know will be quite simple but I have not found the naswer on google yet.

May 21, 2011, 03:17 AM
That syntax is not really objective-c. I suggest the links & stickies on the top of this forum here and get started with the basics. Pick up a good book.

May 21, 2011, 06:44 AM
dantastic, I am working from iphone application development for dummies at the moment. This is actually for a job interview. I have a few days to learn how to make a basic app and this is something I'm stuggling to figure out at the moment. Given a month I could easily do it myself as I do have experience programming in Java. Right now I am trying to figure out how to download a plist from a server, read it in and use that to populate a table. I don't want help with all of this as I like figuring things out myself but help with this small part would be much appreciated :)

EDIT: I also just found a line in the book that says using plist files is beyond the scope of the book and I don't have enough money to by another one now :(

May 21, 2011, 09:12 AM
Right, I'll entertain.

I'm from a Java background myself so I know what's strange from a java point of view.

You're going through a book. You have a plist, you need a dictionary. How would you approach this problem if it would have been Java? Your first post indicate that the file is local but the second post indicate you will need to access the file over a network connection.

The book surely explains how to create a dictionary?

NSDictionary *myDict = [[NSDictionary alloc] initWith...];

Here's problem 1, a dictionary is immutable so you create it once and you can't add or remove objects. For a Java like dictionary you need to use NSMutableDictionary

NSMutableDictionary *myDict = [[NSMutableDictionary alloc] init];

But as a seasoned Java programmer you should have been able to find that in about 2 sec using google.

So you have a NSMutableDictionary now, how will you get the file into the 'myDict'?

May 21, 2011, 09:59 AM
I wouldn't say seasoned, I'm a recent graduate who has spent most of my time during my degree making java apps, some for fun and some for course work.

I am not worried about downloading the file at the moment as thats what I'm going to figure out once I know how to actually read in the file in a way that I can use it.

I found the following code online which lets me read in the plist file and output it's contents to the console:

[window makeKeyAndVisible];
NSDictionary *dictionary;

NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path StringByAppendingPathComponent:@"productList.plist"];
dictionary = [NSDictionary dictionaryWithContentsOfFile:finalPath];

for (id key in dictionary) {
NSLog(@"bundle: key=%@, value=%@", key, [dictionary objectForKey:key]);

So now I have the file read into a local dictionary (it doesn't matter that it's immutable I don't need to change any of the data inside it, only read it in). All I need to figure out now is how to read a specific entry in the dictionary and I can write my loop to read it.

My original question doesn't matter anymore as a since I have never used dictionaries before I misunderstood the nature of them thinking I had to create an array in order to loop through every entry.

What I need to know now is how to read an individual entry in the dictionary. So one last question, what do the opperators "%@" mean in this context? The rest I can figure out myself :)

May 21, 2011, 10:17 AM
You _really_ want to have a look at the documentation - it's part of xcode.

Accessing a single value in a dictionary is done by

WhateverClass *myWhatever = [myDict valueForKey:@"Some Key"];

I'm using WhateverClass here as I've no idea what's in the dictionary... there are handy functions you can use to get a list of all possible keys for the dictionary as well but they are very well documented in the documentation for NSDictionary so I won't duplicate here.

the %@ in the context means an object, in the code posted the first occurance of %@ will be replaced by the value of "key" the second occurrence will be replaced by the value of "[dictionary objectForKey:key]"

In the sample these are most likely NSStrings. the following should explain it better:

NSInterger myInt = 5;
float myFloat = 2.5;
NSString *text = @"Hello";
NSLog(@"%@ World. %f * 2 = %i", text, myFloat, myInt);

>Hello World. 2.5 * 2 = 5

May 21, 2011, 11:12 AM
Thanks so much for that, at your request I read some of the documentation so I now understand how to use both valueForKey and objectForKey properly. I have written the following code which does what I wanted based on your advice:

for (id key in dictionary) {
tempDict = [dictionary objectForKey:key];
NSLog(@"Product Name = %@", [tempDictvalueForKey:@"name"]);
[tempDict release];

The dictionary is made up of other dictionaries which each represent a product. These in turn are made up of 3 NSStrings onc of which contains the product name.

So now that I can read the data in the plist file easily, I'm moving on to learn how to download the file to the iphones documents folder. Hopefully I'll be able do do that myself without needing the help of this forum :D