swift-dictionary-merging/merge

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]
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容