简体   繁体   English

类型“Any”不符合协议“Sequence”

[英]Type 'Any' doesn't conform to protocol 'Sequence'

I am having troubles in my codes, when trying to parse JSON data (each data of an array, like how it should be done) and trying to set up the for in loop, the error comes out.我在我的代码中遇到了问题,当尝试解析 JSON 数据(数组的每个数据,比如它应该如何完成)并尝试设置 for in 循环时,错误出现了。 Here's my code这是我的代码

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)

        }

    }

}

Specify the type of jsonDataArray to directly [[String: Any]] and try like this.将jsonDataArray的类型直接指定为[[String: Any]] ,这样试试。

if let jsonDataArray = try? JSONSerialization.jsonObject(with: data!, options: []) as? [[String: Any]] {
     for eachData in jsonDataArray {
         let eachStop = busStops(jsonDataDictiony: jsonDataDictionary)
     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM