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

发布于 2024-11-01 03:05:47 字数 131 浏览 5 评论 0原文

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

只有一腔孤勇 2024-11-08 03:05:47

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

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

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

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

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

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):

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:

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

The DTD -among other things- describes the "objects" and datatypes that the XML file can contain.

旧时光的容颜 2024-11-08 03:05:47

Plist 是属性列表的缩写。它只是苹果用来存储数据的一种文件类型。

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

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

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

翻了热茶 2024-11-08 03:05:47

有关 2021 年 2 月的 @AdamH 的回答的更多信息

  1. propertyListFromData 已被 dataWithPropertyList 弃用 自 iOS 8.0 macOS 10.10

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

,将设备的网络配置设置转储到 stdout

dump_plist("/private/var/preferences/SystemConfiguration/preferences.plist");

A little more on @AdamH's answer from Feb. 2021

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

  2. Apple seems to be incorrect. It should be propertyListWithData instead of 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);
}}

E.g. to dump device's network configuration settings to stdout

dump_plist("/private/var/preferences/SystemConfiguration/preferences.plist");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文