【问题标题】:Plist: what it is and how to use itPlist:它是什么以及如何使用它
【发布时间】:2011-04-13 19:25:21
【问题描述】:

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

【问题讨论】:

    标签: objective-c xcode plist


    【解决方案1】:

    您可以使用以下代码轻松地将 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 文件可以包含的“对象”和数据类型。

    【讨论】:

      【解决方案2】:

      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

      【讨论】:

      【解决方案3】:

      更多关于 @AdamH's answer 的信息,从 2021 年 2 月开始

      1. propertyListFromData has been deprecated by 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);
      }}
      

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-07-04
        • 2022-01-10
        • 2015-03-13
        • 2013-10-25
        • 2014-11-19
        • 1970-01-01
        相关资源
        最近更新 更多