How to concatenate two dictionaries with a += operator overload

Viewed 3128

I would like to concatenate two dictionaries with a += operator overload using the method below.

static func += <Key, Value> ( left: inout [Key : Value], right: [Key : Value]) {
    for (key, value) in right {
        left.updateValue(value, forKey: key)
    }
}

OR

static func +=<Key, Value>( left: inout Dictionary<Key ,Value>, right: Dictionary<Key, Value>) {
    for (key, value) in right {
        left.updateValue(value, forKey: key)
    }
}

With this implementation:

var properties = ["Key": "Value"]
var newProperties = ["NewKey": "NewValue"]
properties += newProperties

I get below error from xCode,

Cannot convert value of type '[String: Any]' to expected argument type 'inout [_ : ]' (aka 'inout'Dictionary<, _>)

It doesn't work, can anyone can help me or if it's not possible, explain me why?

2 Answers
Related