How to append elements into a dictionary in Swift?

后端 未结 19 2000
旧时难觅i
旧时难觅i 2020-11-28 23:31

I have a simple Dictionary which is defined like:

var dict : NSDictionary = [ 1 : \"abc\", 2 : \"cde\"]

Now I want to add an element into t

19条回答
  •  余生分开走
    2020-11-28 23:59

    I know this might be coming very late, but it may prove useful to someone. So for appending key value pairs to dictionaries in swift, you can use updateValue(value: , forKey: ) method as follows :

    var dict = [ 1 : "abc", 2 : "cde"]
    dict.updateValue("efg", forKey: 3)
    print(dict)
    

提交回复
热议问题