Plist:它是什么以及如何使用它

时间:2011-04-13 19:25:21

标签: objective-c xcode plist

.plist文件究竟是什么?我将如何使用它?当我在xcode中查看它时,它似乎生成某种模板vs向我展示一些xml代码。有没有办法通过将内容推送到数组中来提取plist文件中的数据?另外,我在哪里可以查看.plist的来源?

4 个答案:

答案 0 :(得分:13)

您可以使用以下代码轻松地将plist的内容放入数组中(我们在这里打开名为'file.plist'的文件,这是Xcode项目的一部分):

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

plist只是一个XML文件,对应于Apple设计的一些DTD(数据类型字典),DTD可以在这里看到:

  

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

DTD - 其他东西 - 描述了XML文件可以包含的“对象”和数据类型。

答案 1 :(得分:7)

Plist是物业清单的缩写。它只是Apple用来存储数据的文件类型。

您可以在此处获取更多信息:

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

如果您想阅读plists,请点击此处:

// 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可以是plist中的顶级容器。例如,如果plist是字典,那么plist将是NSDictionary。如果plist是一个数组,它将是NSArray

格式枚举:

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

答案 2 :(得分:0)

.plist或“属性列表”

.plist是属性列表的缩写,它是每个Xcode项目自动创建的文件。此文件在运行时(应用程序运行时)存储该应用程序的配置信息。信息以称为键值对的格式存储。它类似于字典,键是属性名称,值是配置。

这里是Apply says about it

答案 3 :(得分:0)

2021 年 2 月起 @AdamH's answer 的更多信息

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

  2. 苹果似乎不正确。它应该是 propertyListWithData 而不是 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);
}}

例如将设备的网络配置设置转储到标准输出

dump_plist("/private/var/preferences/SystemConfiguration/preferences.plist");
相关问题