Daniel Zapata Daniel Zapata - 7 months ago 9
Swift Question

Iterate over AnyObject. Error: Type 'AnyObject' does not conform to protocol 'SequenceType'

I'm using Alamofire to get a data from a JSON file. Example of the output:
[{"image_name":"vacation"},{"image_name":"graduation"}]

I have a problem when I try to access the information from the JSON output.

Alamofire.request(.GET, url).responseJSON { (response) -> Void in
if let JSON = response.result.value {
for json in JSON{
print(json)
}
}


The problem I have is that my JSON output is an AnyObject and I cannt iterate over an AnyObject. If I do the following:

print(JSON[0]["image_name"])


Then I can see the output correctly. How can I iterate over an AnyObject?

JAL JAL
Answer

You may need to explicitly state the type of JSON as an array of dictionaries:

if let JSON = response.result.value as [[String : AnyObject]] {
    // ...
}
Comments