参数类型' AnyObject'不符合预期类型NSCopying

时间:2016-04-12 20:16:16

标签: ios swift cocoa-touch nsdictionary nsmutabledictionary

我正在NSDictionary中使用Swift,我正面临着上述问题。我有一个以下格式的字典:

let xyz: NSMutableDictionary = ["1":[1,2,3,4,"1","n","1","2"],"2":[1,2,3,4,"+","o","6","2"]]

我想迭代字典中的键并提取数组的第6个元素。我试过以下几点;但没有遇到任何运气:

for keys in dictKeyMutableDict {
    let xCentVal = xyz[keys as! [NSCopying]][6]
}

我一直收到下标错误,如果我删除as! [NSCopying],我会收到上述错误。有谁知道如何处理这种情况?

1 个答案:

答案 0 :(得分:0)

删除NSMutableDictionary并将其设为var变为可变。现在,您可以删除as! [NSCopying]

var xyz = ["1":[1,2,3,4,"1","n","1","2"],"2":[1,2,3,4,"+","o","6","2"]]

for keys in dictKeyMutableDict {
    let xCentVal = xyz[keys]![6]
}

或者更好的选择打开它:

for keys in dictKeyMutableDict {
    if let v = xyz[keys] {
        let xCentVal = v[6]
    }
}