【问题标题】:Parsing PLIST in iOS在 iOS 中解析 PLIST
【发布时间】:2012-08-12 21:40:55
【问题描述】:

我正在构建一个本质上是一个目录的 PLIST。我想解析 plist,以便我可以在表格视图中显示姓氏,并在选择时用地址、电话号码和电子邮件等内容填充下一个视图的标签。我可以使用一些指导,这是我迄今为止创建的内容:

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <array> <array> <string>Last Name</string> <string>First Name</string> <string>Address</string> <string>Phone Number</string> <string>Email</string> </array> <array> <string>Last Name</string> <string>First Name</string> <string>Address</string> <string>Phone Number</string> <string>Email</string> </array> <array> <string>Last Name</string> <string>First Name</string> <string>Address</string> <string>Phone Number</string> <string>Email</string> </array>

此 plist 设置是否正确,或者我需要进行更改。如果设置得当,我只是在解析上有点迷失。我过去曾在播客中使用过 GDATAXML,但不确定它与应用内的 PLIST 有何关系。

【问题讨论】:

    标签: iphone xcode parsing plist gdata


    【解决方案1】:

    在 Swift 3 中:

    if let path = Bundle.main.path(forResource: "FileName", ofType: "plist"), 
       let dict = NSDictionary(contentsOfFile: path) as? [String: AnyObject] {
        // use swift dictionary as normal
    }
    

    如果你的第一个节点是一个数组,你可以像这样使用数组(在这种情况下,数组包含字典元素):

    if let path = Bundle.main.path(forResource: "FileName", ofType: "plist"),
       let array = NSArray(contentsOfFile: path) as? [Dictionary<String, Any>] {
        // use swift array as normal
    }
    

    【讨论】:

      【解决方案2】:

      使用NSXMLParserDocumentation is here.

      基本上你会得到一个带有文件内容的NSDictionary。然后使用

      处理它
      [aDictionary objectForKey:@"foo"];
      

      【讨论】:

      • 有史以来最糟糕的答案!既然有内置方法可以做到这一点,为什么还要使用 NSXML 解析器?
      • 不,不是最差的。内置方法并不总是最好的。想想 nsstringwithcontentsofURL。它非常缓慢,而且不灵活。 NSXMLParser 或一些第三方库会比 contentsOfFile 更快。
      • 你错了。使用 stringWithContentsOfFile: 已被 Apple 优化以与 plist 一起使用。
      【解决方案3】:

      如果要将plist加载到数组或字典中,只需使用以下方法:

      NSArray * myArray = [NSArray arrayWithContentsOfFile: <plist-file-path>];
      
      NSDictionary * myDictionary = [NSDictionary dictionaryWithContentsOfFile: <plist-file-path>];
      

      要在 Xcode 中创建 plist,只需选择 File->New 并从 Resource 部分选择 Property List 模板。然后您将看到属性列表编辑器,因此无需尝试手动编写代码。

      【讨论】:

      • 好的,这样的布局是否可以被解析,还是 plist 中的每个数组都需要唯一命名?
      • 是的,我按照您描述的方式使用 Xcode 制作了我发布的内容。
      • 我想这对你有用 - 如果确实如此,请将其标记为正确!谢谢!!
      【解决方案4】:

      不要尝试从您的 plist 中解析 xml。

      您必须将 plist 传递给 NSDictionary

      NSString *pathToPlist = [NSString stringWithFormat:@"%@/your_plist_file.plist",[[NSBundle mainBundle]resourcePath]];
      NSDictionary *plistDictionary = [NSDictionary dictionaryWithContentsOfFile:pathToPlist];
      
      //now you can access the keys and values:
      for (NSString *key in plistDictionary) 
          NSLog(@"key = %@ and value = %@", key, [plistDictionary objectForKey:key]);
      
      //or pass all values to an array:
      NSArray *valArray = [plistDictionary allValues];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-02
        • 1970-01-01
        • 1970-01-01
        • 2012-12-16
        • 2014-02-20
        相关资源
        最近更新 更多