Reputation: 491
I am using Alamofire to perform my network calls. The return from the response(call below) returns a type ANY. How do i get access to my array of dictionaries?
Almofire documentation:
Alamofire.request("https://httpbin.org/get").responseJSON { response in
debugPrint(response)
if let json = response.result.value {
print("JSON: \(json)")
}
}
I have tried the following:
let json2 = json as! [[String:String]]
JSON:
(
(
{
docURL = "https://httpbin.org/get/rab1Mpub.pdf";
name = "rab1Mpub.pdf";
}
),
(
{
docURL = "https://httpbin.org/get/1Mpublic.pdf";
name = "1Mpublic.pdf";
}
),
(
{
docURL = "https://httpbin.org/get/plantLabBook";
name = "plantLabBook_1.pdf";
}
)
)
I just get the following error:
Could not cast value of type '__NSArrayI' (0x10740c648) to 'NSDictionary' (0x10740b1a8).
2018-03-16 15:46:16.501012+0000 labbook[12637:401862] Could not cast value of type '__NSArrayI' (0x10740c648) to 'NSDictionary' (0x10740b1a8).
Upvotes: 0
Views: 155
Reputation: 20244
You have an array, of an array, of a dictionary:
let json = json as! [[[String:String]]]
for outer in json {
for inner in outer {
for (key, value) in inner {
print(key, "has", value)
}
}
}
NOTE: as!
should be done only if you're absolutely sure of the object type. i.e that the API will always send [[[String:String]]]
.
PS: I personally use SwiftyJSON to make my life easier when I need to work with nested structures.
Upvotes: 1