从plist读取数据

时间:2017-03-24 12:03:54

标签: swift3 plist

我试图将自己的应用从Objective-C转换为Swift 3。 尝试是我的plist样本:

elements.plist

我尝试使用另一个类似问题的代码:

            {

                "Id" : "myEmrCluster",
                "Fields" : [
                    { "Key" : "terminateAfter","StringValue":"1 hours" },
                    { "Key" : "schedule","RefValue" : "theSchedule" },
                    { "Key" : "step","StringValue" : "some.jar,-param1,val1" },
                    { "Key" : "step","StringValue" : "someOther.jar,-foo,bar" }
                ]
            }
       }

请告诉我如何从plist导入数据并访问Swift 3中的单个字段。

1 个答案:

答案 0 :(得分:1)

看看plist。它包含一系列字典。

guard let  plistDicts = try! PropertyListSerialization.propertyList(from: plistData, options: .mutableContainersAndLeaves, format: &format) as? [[String : AnyObject]] 
     else { 
          return 
     }
 // See how it is casted to an Array of Dictionaries ([[String : AnyObject]]) here 

现在你可以迭代你的决定并做你需要的事情:

for dict in plistDicts {
    if let value = plistDict["DescrizioneEsercizio"] as? String {
        //do something with your value
        print(value)
    }
}