gpt4 book ai didi

arrays - Swift - 如何将 Any 转换为 Array\Dictionary?

转载 作者:搜寻专家 更新时间:2023-11-01 06:27:37 24 4
gpt4 key购买 nike

我正在尝试解析一个包含字符串、整数和数组的数组的 json

所以我要迭代数组中的 json {["item1", 2, ["subItem1", "subitem2"] ]} 成员:

func parse(json : [Any])
for item in json{
if let arr = item as? Array { //
//do stuff for array
}
}

但是我得到了这个编译错误:

Generic parameter 'Element' could not be inferred in cast to 'Array<_>'

最佳答案

可选的将 item 绑定(bind)到不同的类型

for item in json {
if let stringItem = item as? String {
//do stuff for String
} else if let intItem = item as? Int {
//do stuff for Int
} else if let arrayItem = item as? [String] {
//do stuff for Array
} else {
// it's something else
}
}

关于arrays - Swift - 如何将 Any 转换为 Array\Dictionary?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51859628/

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