Episode 49 - Property Lists in Cocoa

Episode 49 - Property Lists in Cocoa

Posted 12/02/2008 - 22:28 by cocoacast

Episode-49In this Episode we look at Property Lists. Find out about the types of property lists, when to use them, and how to serialize and deserialize them. Please watch our site for more details. A brief overview of the subject can be found in Serialization in Cocoa - Property Lists.
Please Note: a bug was found in the source for this episode and corresponding article. Keys and values were reversed in setObject, whuch resulted in NSDate and NSNumbers failing to serialize. I have updated the source code and the article, but it's too late for the recording. Please keep these changes in mind.



Here are the show notes for this Episode: Show Notes
View the Podcast
View the iPhone/iPod Touch formated version of the Podcast

Source code for this episode can be found here.

Show Notes:

www.cocoacast.com cocoacast@gmail.com
Property Lists in Cocoa
Episode 49 with Vlad Pasman
Cocoacast
Monday, November 24, 2008

www.cocoacast.com cocoacast@gmail.com
Where to Find Help
• email: cocoacast@gmail.com
• forums: http://www.cocoacast.com/?q=forum
• twitter: cocoacast
• This screencast is based on a “Serialization in Cocoa - Property Lists”
article on CocoaCast page. Source code for this show can also be found
there
Monday, November 24, 2008

www.cocoacast.com cocoacast@gmail.com
What is this show about?
• Serialization is needed to persist the state of an object.
• Cocoa provides several ways to serialize your objects:
• NSArchive allows to serialize arbitrary objects in binary form
• Plists force you to use plist-serializable data types: NSArray,
NSDictionary, NSData, NSDate, NSNumber, and NSString, their
mutable twins, or their Core Foundation equivalents. No code is
necessary, which makes it very convenient to store configuration
properties, that are usually stored in dictionaries, etc.
Monday, November 24, 2008

www.cocoacast.com cocoacast@gmail.com
Plists
• There are several types of property lists: XML, binary, and legacy:
• Legacy property lists store data in a deprecated format going back to
NextSTEP days. That format is currently only supported in read-only
mode and we will not talk about it.
• The default plist format is XML: it generates human readable XML files
that can get quite large, but are easy to modify and debug.
• The binary plist format is much smaller and efficient and can be used to
pass serialized data between multiple applications. Such format is
especially useful for communicating with constrained devices, such as
iPhone.
Monday, November 24, 2008

www.cocoacast.com cocoacast@gmail.com
Plists
Monday, November 24, 2008

www.cocoacast.com cocoacast@gmail.com
Plists
• Plist is an arbitrary structure built of plist-serializable data types. In order
to create a plist, we simply create an NSDictionary or NSArray that
points to other plist-compatible data. Please note that if anything inside
of a plist is not compatible, the entire plist will not be serialized!
• There is an inconstancy in the plist behavior: NSDate and NSNumber
objects do not always serialize, so replace those with NSString if
possible
NSMutableArray *books = [NSMutableArray new];
NSMutableDictionary *book = [NSMutableDictionary new];
[books addObject:book];
NSString *title = [NSString stringWithFormat:@"War and
Peace Volume %d", i+1];
...
BOOL success = [books writeToFile:@"../../inventory.xml"
atomically:NO];
Monday, November 24, 2008

www.cocoacast.com cocoacast@gmail.com
Plists
• NSArray and NSDictionary have writeToFile messages that serialize them
to the default XML format
• There is another way to save a plist to an XML file:
NSData *xmlData = [NSPropertyListSerialization dataFromPropertyList:books
format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
if(xmlData)
{
[xmlData writeToFile:@"/Users/vpasman/test1.plist" atomically:YES];
}
else
{
NSLog(error);
[error release];
}
Monday, November 24, 2008

Plists

• Same, but to a binary file
www.cocoacast.com cocoacast@gmail.com
NSData *xmlData = [NSPropertyListSerialization dataFromPropertyList:books
format:NSPropertyListBinaryFormat_v1_0 errorDescription:&error];
if(xmlData)
{
[xmlData writeToFile:@"/Users/vpasman/test1.plist" atomically:YES];
}
else
{
NSLog(error);
[error release];
}
Monday, November 24, 2008

www.cocoacast.com cocoacast@gmail.com
Plists
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:@"/Users/vpasman/test.plist"];
• Just like for saving, NSArray and NSDictionary have messages for loading
plists in default XML format
• If instead, we use NSMutableArray and NSMutableDictionary, we will be
able to modify the contents of the container and write it out to a file again
• In practice, it doesn’t matter whether we load into a mutable or non-
mutable container -- you will still be able to modify the contents of a non-
mutable container (despite compiler warnings). It has to do with the
implementation of the deserializer.
Monday, November 24, 2008

www.cocoacast.com cocoacast@gmail.com
• There IS a way to enforce the [im]mutability of a loaded plist -- through
mutabilityOption. Use NSPropertyListImmutable for immutability and
NSPropertyListMutableContainersAndLeaves for mutability
Plists
NSPropertyListFormat format;
books = [NSPropertyListSerialization propertyListFromData:binData
mutabilityOption:NSPropertyListImmutable format:&format
errorDescription:&error];
• If you now attempt to modify an immutable collection, you will get a run-time
exception
Monday, November 24, 2008

www.cocoacast.com cocoacast@gmail.com
Demo
Plist demo
Monday, November 24, 2008

www.cocoacast.com cocoacast@gmail.com
Challenge
Create a TCP-based client server and pass Book Inventory Data data via a binary plist
Monday, November 24, 2008

www.cocoacast.com cocoacast@gmail.com
Next Time
• Where to find help:
cocoacast@gmail.com
http://www.cocoacast.com/?q=forum
http://www.twitter.com/cocoacast
• Future Topics Include:
NSStreams, Web Services, Bonjour, Core Graphics, Objective C 2.0, and
more.
• Please post on our forums or email and tell us what you want to hear
about the most. WE LISTEN!
Monday, November 24, 2008

“ Do, or do not.There is no try. ”
Monday, November 24, 2008

Trackback URL for this post: http://www.cocoacast.com/?q=trackback/186
0
Your rating: None