dictionary - 如何将项目字典添加到另一个字典中

标签 dictionary swift

Swift 中的数组支持 += 运算符将一个数组的内容添加到另一个数组。有没有一种简单的方法可以对字典执行此操作?

例如:

var dict1 = ["a" : "foo"]
var dict2 = ["b" : "bar"]

var combinedDict = ... (some way of combining dict1 & dict2 without looping)

最佳答案

你可以为Dictionary定义+=运算符,例如,

func += <K, V> (left: inout [K:V], right: [K:V]) { 
    for (k, v) in right { 
        left[k] = v
    } 
}

关于dictionary - 如何将项目字典添加到另一个字典中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35405199/

相关文章:

java - 词库/词干词典

python - 子列表每个位置的正则表达式模式的频率

java - ASCII 世界地图导入和读取

python - 值错误 : too many values to unpack in Python Dictionary

ios - 使用 SFSafariViewController 下载文档

swift - Xcode 问题 : Library not loaded: @rpath/libswiftAppKit. dylib

python - 在 Python 中使用数组递归查找字典中的元素

ios - 如何只保留 UITextField 的下边框?

ios - 在attributeText中添加链接修饰符后,范围内的文本变得不可见

swift - 谷歌地图 SDK 错误