Plist: what it is and how to use it
Asked Answered
F

3

8

What exactly is a .plist file and how would I use it? When I view this in xcode, it seems to generate some kind of template vs showing me some xml code. Is there a way that I can extract the data in a plist file by pushing the contents into an array? Also, where can I view the source of the .plist?

Francoisefrancolin answered 13/4, 2011 at 19:25 Comment(0)
H
14

You can easily get the contents of a plist into an array by using the following code (we're opening here the file called 'file.plist' that's part of the Xcode project):

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"file" ofType:@"plist"];
contentArray = [NSArray arrayWithContentsOfFile:filePath];

A plist is just a XML file which corresponds to some DTD (datatype dictionary) designed by Apple, the DTD can be seen here:

http://www.apple.com/DTDs/PropertyList-1.0.dtd

The DTD -among other things- describes the "objects" and datatypes that the XML file can contain.

Hardtop answered 13/4, 2011 at 19:29 Comment(0)
G
7

Plist is short for property list. It is just a filetype used by Apple to store data.

You can get more info here:

http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man5/plist.5.html

If you want to read in plists check here:

// Get the location of the plist
// NSBundle represents the main application bundle (.app) so this is a shortcut
// to avoid hardcoding paths
// "Data" is the name of the plist
NSString *path = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];

// NSData is just a buffer with the binary data
NSData *plistData = [NSData dataWithContentsOfFile:path];

// Error object that will be populated if there was a parsing error
NSString *error;

// Property list format (see below)
NSPropertyListFormat format;

id plist;

plist = [NSPropertyListSerialization propertyListFromData:plistData
                                mutabilityOption:NSPropertyListImmutable
                                format:&format
                                errorDescription:&error];

plist could be whatever the top level container in the plist was. For example, if the plist was a dictionary then plist would be an NSDictionary. If the plist was an array it would be an NSArray

Here the format enum:

enum {
   NSPropertyListOpenStepFormat = kCFPropertyListOpenStepFormat,
   NSPropertyListXMLFormat_v1_0 = kCFPropertyListXMLFormat_v1_0,
   NSPropertyListBinaryFormat_v1_0 = kCFPropertyListBinaryFormat_v1_0
}; NSPropertyListFormat;

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/PropertyLists/SerializePlist/SerializePlist.html.html

Graham answered 13/4, 2011 at 19:26 Comment(2)
thanks for the code. Would you be able to tell me line by line what you're doing here? Also I'm not sure what some of these datatypes are (NSBundle, NSData, NSPropertyListFormat, NSPropertyListSerialization).Francoisefrancolin
@Francoisefrancolin search Apple Developer Documentation - NSBundle NSData NSPropertyListFormat NSPropertyListSerializationJenicejeniece
J
0

A little more on @AdamH's answer from Feb. 2021

  1. propertyListFromData has been deprecated by dataWithPropertyList since iOS 8.0 macOS 10.10

  2. Apple seems to be incorrect. It should be propertyListWithData instead of dataWithPropertyList

void dump_plist(const char *const path){@autoreleasepool{
  NSError *err=nil;
  puts([[NSString stringWithFormat:@"%@",[NSPropertyListSerialization
    propertyListWithData:[NSData dataWithContentsOfFile:[[NSString new]initWithUTF8String:path]]
    options:NSPropertyListMutableContainersAndLeaves
    format:nil
    error:&err
  ]]UTF8String]);
  assert(!err);
}}

E.g. to dump device's network configuration settings to stdout

dump_plist("/private/var/preferences/SystemConfiguration/preferences.plist");
Jenicejeniece answered 8/3, 2021 at 4:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.