How to append elements into a dictionary in Swift?

后端 未结 19 1981
旧时难觅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

    Given two dictionaries as below:

    var dic1 = ["a": 1, "c": 2]
    var dic2 = ["e": 3, "f": 4]
    

    Here is how you can add all the items from dic2 to dic1:

    dic2.map {
       dic1[$0.0] = $0.1
    }
    

    Cheers A.

提交回复
热议问题