Search code examples
iosarraysswiftswift3

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. 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)

        }

    }

}

Solution

  • Specify the type of jsonDataArray to directly [[String: Any]] and try like this.

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