How to combine two Dictionary instances in Swift?

前端 未结 10 1918
滥情空心
滥情空心 2020-12-01 04:13

How do I append one Dictionary to another Dictionary using Swift?

I am using the AlamoFire library to send a

10条回答
  •  渐次进展
    2020-12-01 04:39

    var d1 = ["a": "b"]
    var d2 = ["c": "e"]
    
    extension Dictionary {
        mutating func merge(dict: [Key: Value]){
            for (k, v) in dict {
                updateValue(v, forKey: k)
            }
        }
    }
    
    d1.merge(d2)
    

    Refer to the awesome Dollar & Cent project https://github.com/ankurp/Cent/blob/master/Sources/Dictionary.swift

提交回复
热议问题