Home > 2010 Fall DPS913 > Introduction to “property list” (plist)

Introduction to “property list” (plist)

September 23, 2010 Leave a comment Go to comments

Simple data persistence can be done with a “property list”, also know as a plist. It is typically an XML-based storage format, for certain kinds of objects. Arrays or dictionaries can easily be stored in a plist.

.

This approach is good for simple data persistence, and low-volume/low-quantity data persistence.
.

Getting started with a plist

An array can be “round tripped”as a plist. Assuming that “myArray” is an array object, and the string “pathToFile” is a complete path and filename, here’s how you create/write a plist for the array:

    [myArray writeToFile:pathToFile atomically:YES];

Here’s how you open/read a plist into an array (again, assuming “pathToFile” is a string…):

    NSArray *myArray = [NSArray arrayWithContentsOfFile:pathToFile];

A dictionary can also be “round tripped” as a plist. Assume “myDict” is a dictionary object; and again, assuming “pathToFile”, here’s how you create/write a plist for the dictionary:

    [myDict writeToFile:pathToFile atomically:YES];

Here’s how you open/read a plist into a dictionary (again, assuming “pathToFile”…):

    NSDictionary *myDict = [NSDictionary dictionaryWithContentsOfFile:pathToFile];

.

Allowable types in a plist

The following types can be stored in a plist:

  • NSDictionary
  • NSArray
  • NSString
  • NSNumber
  • NSData
  • NSDate

Nesting of NSDictionary and NSArray objects is permitted. This enables you to easily persist a hierarchical data structure.

.


Advertisements
Categories: 2010 Fall DPS913
  1. Thon
    February 21, 2011 at 10:04 am

    This is a great breakdown of the methods but I do have a question. In iOS4 when would we want to read/write to the plist? Would it be during viewDidLoad or using NSNotificationCenter UIApplicationWillTerminateNotification?

    I was able to save and read plists in iOS3 but the technic seems to crash the app in iOS4.

    Thanks for the help and great blog

    • petermcintyre
      March 30, 2011 at 10:52 am

      (Sorry about the response delay; you may already have the answer that you need.)

      When to read/write? The answer is that “it depends”. (I’ll assume that you are attempting to write to the Documents folder.)

      Reading, when the app launches: I think that it’s OK to load in the first view controller’s viewDidLoad method. Alternatively, you can configure the first view controller with a data-bearing property. Then, you can fetch the data (from the plist) in the app delegate, and pass the data to the view controller during initialization.

      Writing: Do this when it makes sense for your app. It may make sense to write often, but typically you do writes when there’s any risk of data loss. The app delegate’s applicationDidEnterBackground: method may be a good place to handle this. Additionally, if a view controller has unsaved changes, and there’s a risk its memory may be released (due to memory pressure), that would suggest a more aggressive stance.

      See this code example for a real simple read/write cycle:
      http://matrix.senecac.on.ca/~peter.mcintyre/dps913/downloads/Week_03_5_Notes_plist.zip

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: