我试图将自己的应用从Objective-C转换为Swift 3。 尝试是我的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中的单个字段。
答案 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)
}
}