xcode Plist:它是什么以及如何使用它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5654470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Plist: what it is and how to use it
提问by locoboy
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?
.plist 文件究竟是什么,我将如何使用它?当我在 xcode 中查看它时,它似乎生成了某种模板,而不是向我展示一些 xml 代码。有没有办法通过将内容推送到数组中来提取 plist 文件中的数据?另外,在哪里可以查看 .plist 的来源?
回答by Wolfgang Schreurs
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):
您可以使用以下代码轻松地将 plist 的内容放入数组中(我们在这里打开名为“file.plist”的文件,它是 Xcode 项目的一部分):
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:
plist 只是一个 XML 文件,它对应于 Apple 设计的一些 DTD(数据类型字典),DTD 可以在这里看到:
The DTD -among other things- describes the "objects" and datatypes that the XML file can contain.
DTD(除其他外)描述了 XML 文件可以包含的“对象”和数据类型。
回答by AdamH
Plist is short for property list. It is just a filetype used by Apple to store data.
Plist 是属性列表的缩写。它只是 Apple 用来存储数据的一种文件类型。
You can get more info here:
您可以在此处获取更多信息:
http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man5/plist.5.html
http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man5/plist.5.html
If you want to read in plists check here:
如果您想在 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
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
plist
可以是 plist 中的任何顶级容器。例如,如果 plist 是一个字典,那么plist
它将是一个NSDictionary
. 如果 plist 是一个数组,它将是一个NSArray
Here the format enum:
这里的格式枚举:
enum {
NSPropertyListOpenStepFormat = kCFPropertyListOpenStepFormat,
NSPropertyListXMLFormat_v1_0 = kCFPropertyListXMLFormat_v1_0,
NSPropertyListBinaryFormat_v1_0 = kCFPropertyListBinaryFormat_v1_0
}; NSPropertyListFormat;
回答by Xab Ion
.plist or The Property List
.plist 或属性列表
.plist is short for Property List and it's file that's automatically created with every Xcode project. This file stores configuration information for the application at runtime (when the app is running). The information is stored in a format called a key-value pair. Its similar to a dictionary, the key is the property name and the value is the configuration.
.plist 是 Property List 的缩写,它是随每个 Xcode 项目自动创建的文件。此文件存储应用程序在运行时(应用程序运行时)的配置信息。信息以一种称为键值对的格式存储。它类似于字典,键是属性名,值是配置。
Here's What the Apply says about it.
这是应用程序对此的说明。