The argument type "AnyObject" does not match the expected type of NSCopying

I am trying to use NSDictionaryin Swift, and I am facing the above problem. I have a dictionary of the following format:

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

I want to iterate over the keys in a dictionary and extract the 6th element of the array. I tried the following: but no luck:

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

I keep getting the index error, and if I delete as! [NSCopying], I get the above error. Does anyone know how to deal with such a case?

0
source share
1 answer

Delete NSMutableDictionaryand make it mutable by making it var. Now you can deleteas! [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]
    }
}
0

Source: https://habr.com/ru/post/1657049/


All Articles