Pages

Thursday, July 29, 2010

iOS Property Lists Tutorial

Property Lists (or plists) are one of the most common persistent data stores on the iOS platform. They are small and fast and good enough for many things you might like to store, like user attributes, game settings, high scores, etc. They are both easy to read and write from/to into native data objects (a small list of the most common ones) Here is how you can create and read/write the lists easily.

For a comprehensive treatment on the subject refer to Apple's website.

Creating Property lists


1. Let's start by creating a simple plist of names of gadgets. Start a new Xcode project. Right click on Resources folder and choose Add > New File. On the left panel under Mac OS X choose Resource, then select Property List before clicking on Next. Call it gadget-names.

2. Open your gadget-names.plist in Xcode. Change the Type to Array. Now click on the tab sticking out at the end. You can now enter the values. Click on the plus symbol now to add new items. Add the following list, preserving the order of the items:
Google Nexus One
Google Nexus S
iPhone
iPhone 3G
iPhone 3GS
iPhone 4
Macbook
Macbook Pro
Macbook Air
Do a Save before proceeding.

Read from Property Lists

3. Next we are going to read the data from gadget-names.plist into a NSArray. We will do it in didFinishLaunchingWithOptions method in the AppDelegate of your application. Then we will output the Array through NSLog to have a look at the contents of the Array:
NSArray *plistArray;
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"gadget-names.plist"];
plistArray = [NSArray arrayWithContentsOfFile:finalPath];
NSLog(@"%@", plistArray);

Run your application and open up Console and you should be able to see contents of the array.

4. Next we will create a plist dictionary called gadgetDict.plist. Repeat the procedure for creating one as in step 2. Instead of Array choose Dictionary. Enter the following key/value pairs:
"Google Nexus One" , "http://upload.wikimedia.org/wikipedia/en/6/61/
Google_HTC_Nexus_One_front.jpg"
"Google Nexus S" , "http://upload.wikimedia.org/wikipedia/en/8/8f/
Google_Nexus_S_front.jpg"
Macbook , "http://upload.wikimedia.org/wikipedia/commons/thumb/6/63/
MacBook_white.png/220px-MacBook_white.png"
"Macbook Air" , "http://upload.wikimedia.org/wikipedia/commons/thumb
/6/65/Late_2010_MacBook_Air_edit.png/200px-Late_2010_MacBook_Air_edit.png";
"Macbook Pro" , "http://upload.wikimedia.org/wikipedia/commons/thumb
/8/8e/MacBook_Pros.jpg/300px-MacBook_Pros.jpg"
iPhone , "http://upload.wikimedia.org/wikipedia/commons/thumb
/d/d0/Original_iPhone_docked.jpg/250px-Original_iPhone_docked.jpg"
"iPhone 3G" , "http://upload.wikimedia.org/wikipedia/en/8/82
/Apple_iPhone_3G_front.jpg"
"iPhone 3GS" = "http://upload.wikimedia.org/wikipedia/en/b/b2
/Apple_iPhone_3GS_front.jpg";
"iPhone 4" = "http://upload.wikimedia.org/wikipedia/en/thumb
/b/b8/Apple_iPhone_4_front.jpg/200px-Apple_iPhone_4_front.jpg"
Note that as this is a dictionary the order you enter them in does not matter nor should you expect the Dictionary to be ordered in any particular sequence.

5. As with gadget-names.plist we are going to read gadgetDict into an NSObject, an NSDictionary this time. We will do it in didFinishLaunchingWithOptions method in the AppDelegate of your application. Then we will output through NSLog to have a look at the contents of the Dictionary:
NSDictionary *plistDict;
path = [[NSBundle mainBundle] bundlePath];
finalPath = [path stringByAppendingPathComponent:@"gadgetDict.plist"];
plistDict = [NSDictionary dictionaryWithContentsOfFile:finalPath];
NSLog(@"%@", plistDict);

Run your application and refer to console and you should be able to see the output from the Dictionary.

No comments:

Post a Comment