作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试解析一个包含字符串、整数和数组的数组的 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/
我是一名优秀的程序员,十分优秀!