嗨我试图访问我的plist里面的leg数组中的项目 我希望能够获得key =" title"的值。来自此plist
的leg数组我已经有了从字典中获取名为abs的值的方法,这是以下代码
let path = Bundle.main.path(forResource:"Exercise", ofType: "plist")
let dict:NSDictionary = NSDictionary(contentsOfFile: path!)!
if (dict.object(forKey: "levels") != nil) {
if let levelDict:[String : Any] = dict.object(forKey: "levels") as? [String : Any] {
for (key, value) in levelDict {
if ( key == "something") {
print(value)
}
}
}
}
但是我不知道如何访问leg数组来获取项目标题
答案 0 :(得分:0)
所以我设法找到了原来问题的答案
let path = Bundle.main.path(forResource: "Exercise", ofType: "plist")
let dict = NSDictionary(contentsOfFile: path!)!
let levelArray:AnyObject = dict.object(forKey: "legs")! as AnyObject
if let nsArray:NSArray = levelArray as? NSArray{
var levelDict:AnyObject = nsArray[1] as AnyObject //currentLevel is an Int
//then finally I could get data from a dictionary in the array
let Title = levelDict["title"] as AnyObject? as! String
print(Title)
}
所以现在这将打印leg数组中的第二项,即“leg 2”
答案 1 :(得分:0)
在你的情况下到达具有三个元素且每个索引具有两个键对值的类型[[String:String]]
的leg数组。您可以遍历该索引并获取您的值,然后迭代到其他人。
let yourLegsArray:AnyObject = dict.object(forKey: "legs")! as AnyObject
然后迭代yourLegsArray
并获取Dict
的内部值。