Ronald Aja Ronald Aja - 2 months ago 5
iOS Question

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)

}

}

}

Answer

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

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