[Solved] How to access the second element of an array in a dictionary within an array?


This data structure is very difficult to deal with. I would consider if this is the best way to store this data. Maybe an array of structs that represent your data?

The confusing part is your variable is named array, but it is really declared as a Dictionary literal. So to access the item with key “1” in array looks like this:

let dictionaryEntry = array["1"]   

…which is going to return an optional array of strings “First Whatever”, “Second Whatever” – because there is no guarantee it will find “1” in the dictionary.

The dictionary contains an Array of Strings, so to get a single value, for example the 2nd value in the String Array, it would be:

let  dict = array["1"]?[1]  //returns "Second Whatever" as an optional

Again, this is how you access the items in your collection, but I’d strongly review how the data is being stored.

solved How to access the second element of an array in a dictionary within an array?