我有plist
个文件,dictionary
类型,并尝试阅读其内容:
循环遍历NSDictionary
:
let inputFile = NSBundle.mainBundle().pathForResource("frames", ofType: "plist")
let inputDataDictionary = NSDictionary(contentsOfFile: inputFile!)
for inputItem in inputDataDictionary {
println(inputItem)
}
但它说:
'NSDictionary?' does not have a member named 'Generator'
答案 0 :(得分:2)
问题是inputDataDictionary
是可选的。您必须通过!
打开它或在枚举前用if let
检查值。另请注意,迭代字典的推荐方法是通过元组。像这样:
if let dict = inputDataDictionary {
for (key, value) in dict {
println("\(key), \(value)")
}
}
答案 1 :(得分:1)
尝试:
for inputItem in inputDataDictionary.allKeys {
println(inputItem)
}
或者您也可以使用:
for (key, object) in inputDataDictionary {
println(key)
println(object)
}