Instance MethodSwift
merge(_:uniquingKeysWith:)
Merges the key-value pairs in the given sequence into the dictionary, using a combining closure to determine the value for any duplicate keys.
Available when
Key
conforms toHashable
mutating func merge<S>(_ other: S, uniquingKeysWith combine: (Value, Value) throws -> Value) rethrows where S : Sequence, S.Element == (Key, Value)
Parameters
- other
A sequence of key-value pairs.
- combine
A closure that takes the current and new values for any duplicate keys. The closure returns the desired value for the final dictionary.
Overview
Use the combine
closure to select a value to use in the updated dictionary, or to combine existing and new values. As the key-value pairs are merged with the dictionary, the combine
closure is called with the current and new values for any duplicate keys that are encountered.
This example shows how to choose the current or new values for any duplicate keys:
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]