参数类型"AnyObject"不符合预期的类型NSCopying [英] Argument Type 'AnyObject' does not conform to expected type NSCopying

查看:53
本文介绍了参数类型"AnyObject"不符合预期的类型NSCopying的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Swift 中使用 NSDictionary ,并且遇到了上述问题.我有以下格式的字典:

I am trying to use NSDictionary in Swift and I am facing the above-mentioned 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"]]

我想遍历字典中的键并提取数组的第6个元素.我尝试了以下方法;但没有碰到运气:

I want to iterate over keys in the dictionary and extract the 6th element of the array. I tried the following; but did not meet with any luck:

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

我继续收到下标错误,并且如果我删除 as![NSCopying] ,我收到上述错误.有人知道如何处理这种情况吗?

I keep on receiving a subscript error and if I remove as! [NSCopying], I receive the above error. Does anyone know how to deal with such case?

推荐答案

删除 NSMutableDictionary 并将其可变为可变变量.现在您可以删除 as![NSCopying]

Remove NSMutableDictionary and make it mutable by make it a var. Now you can remove the 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]
    }
}

这篇关于参数类型"AnyObject"不符合预期的类型NSCopying的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆