Swift:如何组合两个Dictionary实例? [英] Swift: how to combine two Dictionary instances?
本文介绍了Swift:如何组合两个Dictionary实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将 [Dictionary]
附加到另一个 [Dictionary]
使用 Swift
?
How do I append one [Dictionary]
to another [Dictionary]
using Swift
?
我正在使用 AlamoFire
库发送 JSON
到 REST服务器
。
字典1
let dict1: [String: AnyObject] = [
kFacebook: [
kToken: token
]
]
字典2
let dict2: [String: AnyObject] = [
kRequest: [
kTargetUserId: userId
]
]
如何组合这两个字典以实现如下? / strong>
How do I combine the two dictionaries to achieve like below?
let parameters: [String: AnyObject] = [
kFacebook: [
kToken: token
],
kRequest: [
kTargetUserId: userId
]
]
我尝试过 dict1 + = dict2
但是收到一个编译错误。
I have tried dict1 += dict2
but got a compile error.
提前感谢
推荐答案
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)
请参阅真棒美元& Cent项目
https://github.com/ankurp/ Cent / blob / master / Sources / Dictionary.swift
Refer to the awesome Dollar & Cent project https://github.com/ankurp/Cent/blob/master/Sources/Dictionary.swift
这篇关于Swift:如何组合两个Dictionary实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文