Nested dictionary syntax

I'm storing year/month/day info into a nested dictionary. For the life of me, I can't figure out the syntax to read the day info back out. It's structured like this:

var dict = [String:[[String:[String]]]]()

dict["2016"] = [["Sep":["1","6","20"]]]
dict["2016"]? += [["Oct":["2"]]]
dict["2017"] = [["Jan":["3","7"]]]
dict["2017"]? += [["Feb":["4","19"]]]


I thought that I could read the day for an entry like this:

dict["2016"]?[["Sep"]] // error

But that gives me the error "Cannot subscript a value of type '[[String: [String]]]' with an index of type '[String]'

I know there are probably better ways of structuring this data, but I'd love to know how to handle this!


Thanks

Answered by Claude31 in 196985022

Your structure is a dictionary or arrays of dictionary.


To read the days, you have to write :


let y = dict["2016"]![0]["Sep"]


you get y : ["1", "6", "20"]

Accepted Answer

Your structure is a dictionary or arrays of dictionary.


To read the days, you have to write :


let y = dict["2016"]![0]["Sep"]


you get y : ["1", "6", "20"]

Wow, thanks. That's been driving me insane! It also tells me that I really need to model this differently.


Chuck Lane

Nested dictionary syntax
 
 
Q