user4938361
user4938361

Reputation: 149

Trying to access key in Nested Array of dictionaries in Swift

Perhaps I didn't phrase the question correctly but I need access to keys in an Array of dictionaries that are nested inside of another array of dictionaries. The closest I think I have gotten is

name: dict["name"] as! String,
speciesId: dict["species_id"] as! Int,
identifier: dict["identifier"] as! String,
typeId: dict["forms"]!["item0"]!["type_id"] as! Int

I can grab the first 3 variables but cannot grab the last one as it is nested inside another array of dictionaries. if someone could point me in the right direction for grabbing type_id. And When I tried

typeId: dict["type_id"] as! Int

It just returns as nil on the console.

UPDATE: So going off of one of answers here, using this line to gab an int from an array works

 formId: (dict["egg_groups"] as! Array<Int>)[0] as Int!

but when trying to grab a dict from the array I get EXC_BAD INSTRUCTION

Upvotes: 1

Views: 794

Answers (1)

bunty
bunty

Reputation: 629

Try this and pass correct index as here is 0:

let typeId = (dict["forms"] as! Array<Dictionary<String,Any>>)[0]["type_id"]! as Int

Upvotes: 2

Related Questions