This document describes how to create old-style (non-XML) ASCII property lists and convert them back to property list representations in memory. “Using Old-Style Property Lists in Objective-C” describes how to use old-style property lists in Objective-C, and “Using Old-Style Property Lists in Java” describes how to use them in Java.
Using Old-Style Property Lists in Objective-C
Using Old-Style Property Lists in Java
The NSPropertyListSerialization class (available in Mac OS X v10.2 and later) provides methods for saving and restoring property lists from the three supported formats. To save a property list in the old-style OpenStep format, use the dataFromPropertyList:format:errorDescription: method with NSPropertyListOpenStepFormat as the second parameter. This code fragment saves the property list plist to an old-style ASCII plist file at path:
id plist; // Assume this exists. |
NSString *path; // Assume this is a valid path. |
NSData *plistData; |
NSString *error; |
plistData = [NSPropertyListSerialization dataFromPropertyList:plist |
format:NSPropertyListOpenStepFormat |
errorDescription:&error]; |
if(plistData) |
{ |
NSLog(@"No error creating plist data."); |
[plistData writeToFile:path atomically:YES]; |
} |
else |
{ |
NSLog(error); |
[error release]; |
} |
To restore a property list from data, use the NSPropertyListSerialization method propertyListFromData:mutabilityOption:format:errorDescription:. This code fragment creates an immutable property list from the file at path:
NSString *path; // Assume this is a path to a valid plist. |
NSData *plistData; |
NSString *error; |
NSPropertyListFormat format; |
id plist; |
plistData = [NSData dataWithContentsOfFile:path]; |
plist = [NSPropertyListSerialization propertyListFromData:plistData |
mutabilityOption:NSPropertyListImmutable |
format:&format |
errorDescription:&error]; |
if(!plist) |
{ |
NSLog(error); |
[error release]; |
} |
The NSPropertyListSerialization class provides methods for saving and restoring property lists in the three property list formats. To create old-style ASCII property list representations from a property list, use the stringFromPropertyList static method. The following code fragment stores the property list plist as an old-style plist file at url:
Object plist; // Assume this is a valid property list. |
java.net.URL url; // Assume this is a well-formed local file URL. |
String plistString; |
NSData plistData; |
plistString = NSPropertyListSerialization.stringFromPropertyList(plist); |
plistData = new NSData(plistString.getBytes()); |
plistData.writeToURL(url, true); |
To restore an old-style property list to a property list in memory, use the propertyListFromString static method. The following code fragment restores a property list from the old-style plist file located at url:
java.net.URL url; // Assume this is a URL to a well-formed old-style plist. |
NSData plistData; |
String plistString; |
Object plist; |
plistData = new NSData(url); |
plistString = new String(plistData.bytes(0, plistData.length())); |
plist = NSPropertyListSerialization.propertyListFromString(plistString); |
Last updated: 2006-11-07