Swift Dictionary merging(_:uniquingKeysWith:)用法及代碼示例
用法一
實(shí)例方法
merging(_:<wbr>uniquing<wbr>Keys<wbr>With:)
通過(guò)將給定字典合并到此字典中來(lái)創(chuàng)建字典,使用組合閉包來(lái)確定重復(fù)鍵的值。
聲明
func merging(
_ other: [Key : Value],
uniquingKeysWith combine: (Value, Value) throws -> Value
) rethrows -> [Key : Value]
當(dāng) Key 符合 Hashable 時(shí)可用。
返回值
包含此字典和 other 的組合鍵和值的新字典。
參數(shù)
other 要合并的字典。
combine 為任何重復(fù)鍵獲取當(dāng)前值和新值的閉包。閉包返回最終字典所需的值。
詳述
使用 combine 閉包選擇要在返回字典中使用的值,或組合現(xiàn)有值和新值。隨著other 中的鍵值對(duì)與此字典合并,combine 閉包會(huì)調(diào)用遇到的任何重復(fù)鍵的當(dāng)前值和新值。
此示例顯示如何為任何重復(fù)鍵選擇當(dāng)前值或新值:
let dictionary = ["a": 1, "b": 2]
let otherDictionary = ["a": 3, "b": 4]
let keepingCurrent = dictionary.merging(otherDictionary)
{ (current, _) in current }
// ["b": 2, "a": 1]
let replacingCurrent = dictionary.merging(otherDictionary)
{ (_, new) in new }
// ["b": 4, "a": 3]
用法二
實(shí)例方法
merging(_:<wbr>uniquing<wbr>Keys<wbr>With:)
通過(guò)將序列中的鍵值對(duì)合并到字典中來(lái)創(chuàng)建字典,使用組合閉包來(lái)確定重復(fù)鍵的值。
聲明
func merging<S>(
_ other: S,
uniquingKeysWith combine: (Value, Value) throws -> Value) rethrows -> [Key : Value] where S : Sequence, S.Element == (Key, Value
)
當(dāng) Key 符合 Hashable 時(shí)可用。
返回值
包含此字典和 other 的組合鍵和值的新字典。
參數(shù)
other 一系列鍵值對(duì)。
combine 為任何重復(fù)鍵獲取當(dāng)前值和新值的閉包。閉包返回最終字典所需的值。
詳述
使用 combine 閉包選擇要在返回字典中使用的值,或組合現(xiàn)有值和新值。當(dāng)鍵值對(duì)與字典合并時(shí),combine 閉包會(huì)調(diào)用遇到的任何重復(fù)鍵的當(dāng)前值和新值。
此示例顯示如何為任何重復(fù)鍵選擇當(dāng)前值或新值:
let dictionary = ["a": 1, "b": 2]
let newKeyValues = zip(["a", "b"], [3, 4])
let keepingCurrent = dictionary.merging(newKeyValues) { (current, _) in current }
// ["b": 2, "a": 1]
let replacingCurrent = dictionary.merging(newKeyValues) { (_, new) in new }
// ["b": 4, "a": 3]
Swift Dictionary merge(_:uniquingKeysWith:)用法及代碼示例
用法一
實(shí)例方法
merge(_:<wbr>uniquing<wbr>Keys<wbr>With:)
將給定序列中的鍵值對(duì)合并到字典中,使用組合閉包來(lái)確定任何重復(fù)鍵的值。
聲明
mutating func merge<S>(
_ other: S,
uniquingKeysWith combine: (Value, Value) throws -> Value) rethrows where S : Sequence, S.Element == (Key, Value
)
當(dāng) Key 符合 Hashable 時(shí)可用。
參數(shù)
other 一系列鍵值對(duì)。
combine 為任何重復(fù)鍵獲取當(dāng)前值和新值的閉包。閉包返回最終字典所需的值。
詳述
使用 combine 閉包選擇要在更新字典中使用的值,或組合現(xiàn)有值和新值。當(dāng)鍵值對(duì)與字典合并時(shí),combine 閉包會(huì)調(diào)用遇到的任何重復(fù)鍵的當(dāng)前值和新值。
此示例顯示如何為任何重復(fù)鍵選擇當(dāng)前值或新值:
var dictionary = ["a": 1, "b": 2]
// Keeping existing value for key "a":
dictionary.merge(zip(["a", "c"], [3, 4])) { (current, _) in current }
// ["b": 2, "a": 1, "c": 4]
// Taking the new value for key "a":
dictionary.merge(zip(["a", "d"], [5, 6])) { (_, new) in new }
// ["b": 2, "a": 5, "c": 4, "d": 6]
用法二
實(shí)例方法
merge(_:<wbr>uniquing<wbr>Keys<wbr>With:)
將給定的字典合并到這個(gè)字典中,使用組合閉包來(lái)確定任何重復(fù)鍵的值。
聲明
mutating func merge(
_ other: [Key : Value],
uniquingKeysWith combine: (Value, Value) throws -> Value
) rethrows
當(dāng) Key 符合 Hashable 時(shí)可用。
參數(shù)
other 要合并的字典。
combine 為任何重復(fù)鍵獲取當(dāng)前值和新值的閉包。閉包返回最終字典所需的值。
詳述
使用 combine 閉包選擇要在更新字典中使用的值,或組合現(xiàn)有值和新值。由于 other 中的 key-values 對(duì)與此字典合并,因此使用遇到的任何重復(fù)鍵的當(dāng)前值和新值調(diào)用 combine 閉包。
此示例顯示如何為任何重復(fù)鍵選擇當(dāng)前值或新值:
var dictionary = ["a": 1, "b": 2]
// Keeping existing value for key "a":
dictionary.merge(["a": 3, "c": 4]) { (current, _) in current }
// ["b": 2, "a": 1, "c": 4]
// Taking the new value for key "a":
dictionary.merge(["a": 5, "d": 6]) { (_, new) in new }
// ["b": 2, "a": 5, "c": 4, "d": 6]