iPhone Developer Tips Stats:
|
Reading a plist into an NSArray
Reading a plist into an NSArray
This is the second tip in a series of four that is based on content from the O’Reilly book Head First iPhone Development. The first tip described how to adjust textfields that are hidden when the onscreen keyboard is shown.
O’Reilly and iPhone Developer Tips are collaborating to give away a free ebook (electronic copy) of Head First iPhone Development each Friday in November. You can register in this weeks drawing by referring a friend to this iPhone Developer tip – see the Refer-a-Friend button near the bottom of this post. You can read more about last weeks winner, Bill Mietelski, on this post.
This tip, based on content in Chapter 4 – Multiple View, shows how to read a plist from a file in the resource bundle, into an array.
Hardcoded Array
Based on the example in the book, let’s assume we are keeping a list of mixed drinks inside our application. Somewhere in the initialization process we might write something like the following to build an array of NSString values:
NSMutableArray *array = [[NSMutableArray alloc] initWithObjects: @"Firecracker", @"Lemon Drop", @"Mojito", nil]; // Show the string values for (NSString *str in array) NSLog(@"%@", str);
Creating a plist
Although the code above works, a better solution would be to keep the list of values in a plist as part of the resource bundle, then read the plist into an array.
To create plist, in Xcode, right click on Resources then select Add, New File, Mac OS X, Resource, Property List. Name the file DrinkArray.plist. Change the root type to Array and then add the four string values shown below:

Reading a plist into an NSArray
Reading the plist into an array is straight forward:
// Path to the plist (in the application bundle) NSString *path = [[NSBundle mainBundle] pathForResource: @"DrinkArray" ofType:@"plist"]; // Build the array from the plist NSMutableArray *array2 = [[NSMutableArray alloc] initWithContentsOfFile:path]; // Show the string values for (NSString *str in array2) NSLog(@"--%@", str);
plists and Other Data Types
One of the benefits of working with plists, is that other types, for example NSDictionary, can be read from a plist. This means that you can create more complex data collections in a plist, such as an array of dictionaries.
For example, the plist below is also an array, however, the third value in the array is a dictionary.

With plists you can build various data types needed by your application and read those values directly into your program.









i am using a plist in this way in my app but what i really need to know is how can i make it so the user of the iPhone can add and delete strings in the plist. Can you help please?
Dan,
There is no built in means to allow a user to add/delete entries in the plist. You will need to come up with a means for users to specify values to add and then write those to a plist. Same applies to deleting, you’ll need to have a way for a user to tell you which entries to delete, then write the code to delete those values.