Plist: what it is and how to use it

后端 未结 2 468
慢半拍i
慢半拍i 2021-02-06 06:20

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 ca

2条回答
  •  遥遥无期
    2021-02-06 06:48

    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

提交回复
热议问题