I am wondering why when setting the value of a nested dictionary, the containing dictionary does not reflect those changes? On line3, is a copy of the dictionary being retur
Roman is correct that when you created dic2
, because dictionaries pass by value, you're creating a new dictionary.
You could either copy the dictionary back in, as outlined Access a dictionary in dictionary or you can modify the dictionary in place:
var dic = [String: [String: String]]() // Dictionary>() // line1
dic["key"] = ["a": "a"] // line2
dic["key"]?["a"] = "b" // line4