我找到了此帖子中的代码段:https://stackoverflow.com/a/24052094/2754218并在Playground中对其进行了测试。
func +=<K, V> (inout left: [K: V], right: [K: V]){
for (k, v) in right {
left[k] = v
}
}
var test = ["1": "a"] += ["2": "b"]
代码会导致:Binary operator '+=' cannot be applied to two [String : String] operands.
有什么建议吗?
解决方案:
感谢Eric,我与运营商一起创建了一个功能&#34; +&#34;:
func +<K, V> (left: [K: V], right: [K: V]) -> [K: V] {
var newDic = left
for (k, v) in right {
newDic[k] = v
}
return newDic
}
var toto = ["1": "a"] + ["2": "b"]
答案 0 :(得分:1)
此函数不返回任何内容,它将第一个值作为inout传递,这意味着它将改变左手对象本身:
var test = ["1": "a"]
test += ["2": "b"]
print(test) // ["2": "b", "1": "a"]