2011-04-13 172 views
7

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

回答

13

您可以轻松地使用下面的代码获得的plist的内容到一个数组(我们在这里开叫“file.plist”的文件,该文件的Xcode项目的一部分):

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

一个plist中只是相当于一个XML文件一些DTD(数据类型字典)由苹果设计的DTD可以在这里看到:

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

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

7

Plist是属性列表的简称。这只是Apple用来存储数据的文件类型。

您可以在这里更多的信息:

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

如果你想的Plist阅读点击这里:

// 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

+1

感谢您的代码。你能够逐行告诉我你在做什么吗?此外,我不知道这些数据类型是什么(NSBundle,NSData,NSPropertyListFormat,NSPropertyListSerialization)。 – locoboy 2011-04-13 21:09:56

+0

@ cfarm54我更新了一些更多的内嵌评论 – AdamH 2011-04-14 01:03:32

相关问题