gpt4 book ai didi

ios - 使用 Array 而不是 NSArray 时出现错误 "ambiguous reference to member subscript"

转载 作者:可可西里 更新时间:2023-11-01 00:02:20 25 4
gpt4 key购买 nike

当我尝试访问数组类型值然后出现错误时,我在可能包含数组类型值的字典中收到类似“对成员下标的模糊引用”的错误。请检查以下代码。

var occupations = [
"Malcolm": "Captain",
"Kaylee": "Mechanic",
"Layme": ["Engineer", "Docter"]
] as [String : Any]


occupations["Jayne"] = "Public Relations"
var arrOfLayme = occupations["Layme"] as! Array //getting error here, If I use NSArray instead of array all will work as expacted
print(valueOcc[0])

当我使用 NSArray 类型时,此代码运行良好,如下行所示,我想以纯粹的快速方式进行,不想添加 Objective-c。

var arrOfLayme = occupations["Layme"] as! NSArray

最佳答案

试试这个:

var occupations:[String:Any] = [
"Malcolm": "Captain",
"Kaylee": "Mechanic",
"Layme": ["Engineer", "Docter"]
]

occupations["Jayne"] = "Public Relations"


var arrOfLayme = occupations["Layme"] as! [String]
print(arrOfLayme)

//More safe
if let arr = occupations["Layme"] as? [String] {
print(arr)
}

关于ios - 使用 Array 而不是 NSArray 时出现错误 "ambiguous reference to member subscript",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40991618/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com