我在代码中遇到麻烦,当尝试解析JSON数据(数组的每个数据,应如何完成)并尝试设置for in循环时,错误就会出现。这是我的代码
if let jsonDataArray = try? JSONSerialization.jsonObject(with: data!, options: [])
{
print(jsonDataArray)
var allStops = [busStops]()
for eachData in jsonDataArray
^
//this is where the error is located
{
if let jsonDataDictionary = eachData as? [String : AnyObject]
{
let eachStop = busStops(jsonDataDictiony: jsonDataDictionary)
}
}
}
最佳答案
指定jsonDataArray的类型以直接[[String: Any]]
并尝试这样。
if let jsonDataArray = try? JSONSerialization.jsonObject(with: data!, options: []) as? [[String: Any]] {
for eachData in jsonDataArray {
let eachStop = busStops(jsonDataDictiony: jsonDataDictionary)
}
}
关于ios - 类型“任何”不符合协议(protocol)“序列”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39621023/