How Use Property Lists in Your iOS App - dummies

How Use Property Lists in Your iOS App

By Jesse Feiler

A property list (or plist) is perfect for storing small amounts of data in your iOS app that consist primarily of strings and numbers. What adds to its appeal is the capability to easily read it into your program, use the data, and modify the data and then write the property list back out again. That’s because iOS provides a small set of objects that have that behavior built right in.

Apps and other system software in OS X and iOS use property lists extensively. For example, the OS X Finder stores file and directory attributes in a property list, and iOS uses them for user defaults. You also get a Property List editor with Xcode, which makes property list files easy to create and maintain in your own programs.

After you figure out how to work with property lists, it’s actually easy, but like most things, getting there is half the fun.

Property lists hold serializable objects. A serializable object can convert itself into a stream of bits so that it can be stored in a file; it can then reconstitute itself into the object it once was when it’s read back in.

These objects, called property list objects, that you have to work with are as follows, and you find two types:

Basic Classes:

  • NSData and NSMutableData

  • NSDate

  • NSNumber

  • NSString and NSMutableString

    image0.jpg

Containers:

  • NSArray and NSMutableArray

  • NSDictionary and NSMutableDictionary

Sometimes, an app’s plist is actually an NSDictionary named Root (this is true of all property list unless you change it). Root has one entry — DestinationData, which is an array of dictionaries — and the data for each one of the destinations is held in a dictionary in that array (Item 0 and Item 1).

Now for that explanation of two kinds of property list objects:

  • Basic classes: The term basic classes describes the simplest kind of object. They are what they are.

  • Containers: Containers can hold primitives as well as other containers.

One important feature of property list object containers (such as NSArray and NSDictionary), besides their ability to hold other objects, is that they both have a writeToFile: method that writes the object to a file, and a corresponding initWithContentsOfFile:, which initializes the object with the contents of a file.

So if you create an array or dictionary and fill it chock-full of objects of the property list type, all you have to do to save that array or dictionary to a file is tell it to go save itself — or create an array or dictionary and then tell it to initialize itself from a file.

Primitives NSString and NSData and their mutable counterparts also can write and read themselves to and from a file.

NSData and NSMutableData are wrappers (a wrapper is an object whose basic purpose is to turn something into an object) in which you can dump any kind of digital data and then have that data act as an object.

The containers can contain other containers as well as the primitive types. Thus, you might have an array of dictionaries, and each dictionary might contain other arrays and dictionaries as well as the primitive types.