'的NSDictionary&#39?;没有名为' Generator'的成员

时间:2015-05-21 09:57:09

标签: ios swift

我有plist个文件,dictionary类型,并尝试阅读其内容:

enter image description here

循环遍历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'

enter image description here

2 个答案:

答案 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)
}