A property list is a representation of a hierarchy of objects that can be stored in the file system and reconstituted later. Property lists give applications a lightweight and portable way to store small amounts of data. They are hierarchies of data made from specific types of objects—in effect, an object graph. They are easy to create programmatically and are even easier to serialize into a representation that is persistent. Applications can later read the static representation back into memory and recreate the original hierarchy of objects. Both Cocoa and Core Foundation have APIs related to property-list serialization and deserialization.
Property lists may consist only of certain types of data: dictionaries, arrays, strings, numbers (integer and float), dates, binary data, and Boolean values. Dictionaries and arrays are special types because they are collections; they can contain one or more of these data types, including other dictionaries and arrays. The hierarchical nesting of objects thereby attained creates a graph of objects. The abstract data types have corresponding XML elements and Foundation classes for value objects and collection objects, as shown in the following parallel lists:
Abstract type | Foundation framework class | XML element |
---|---|---|
Array |
| |
Dictionary |
| |
String |
| |
Data |
| |
Date |
| |
Integer |
|
|
Floating-point value |
|
|
Boolean |
|
|
Mutable variants of the Foundation classes are also included in the property list architecture, namely |
Collectively, instances of these classes are known as property-list objects. For example, an NSMutableDictionary
object is a property-list object, as is an NSNumber
object, an NSString
object, and so on. For a property list to be valid, all objects in the object graph must be property-list objects.
You can write property lists out in both XML and binary formats. The binary format is much more compact than the XML version and thus is more efficient. It is recommended for most situations. However, you can manually edit an XML property list if you ever have need to. Property list files have the file-name extension of plist
.
You should not use property lists to store large and complex graphs of objects, especially when the objects have variable mutability settings. And you cannot use property lists to store objects that are not supported by the architecture, such as model objects. For these cases, use archiving instead. Although property lists can include NSData
objects, you should not use data objects to hold large amounts of binary data.
To serialize and deserialize property lists, you call the appropriate class methods of the NSPropertyListSerialization
class or, if using Core Foundation, the facilities related to the CFPropertyListRef
opaque type. Options for serialization allow you to specify XML or binary output. In Cocoa, the serialized output is in the form of an NSData
object. You can therefore use the methods of that class (for example, writeToFile:atomically:
) to write that data to the file system and use the appropriate NSData
class factory memory to read it back into memory. Then, when you deserialize it, you can specify mutability options for the property list.
Last updated: 2010-08-03