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

loc*_*boy 8 xcode objective-c plist

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

Wol*_*urs 14

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

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"file" ofType:@"plist"];
contentArray = [NSArray arrayWithContentsOfFile:filePath];
Run Code Online (Sandbox Code Playgroud)

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

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

DTD - 其他东西 - 描述XML文件可以包含的"对象"和数据类型.


Ada*_*amH 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];
Run Code Online (Sandbox Code Playgroud)

plist可能是plist中的顶级容器.例如,如果plist是一个字典,那么plist将是一个NSDictionary.如果plist是一个数组,它将是一个NSArray

这里格式枚举:

enum {
   NSPropertyListOpenStepFormat = kCFPropertyListOpenStepFormat,
   NSPropertyListXMLFormat_v1_0 = kCFPropertyListXMLFormat_v1_0,
   NSPropertyListBinaryFormat_v1_0 = kCFPropertyListBinaryFormat_v1_0
}; NSPropertyListFormat;
Run Code Online (Sandbox Code Playgroud)

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