Merging 2 Dictionaries in Swift

Updated: May 8, 2023 By: Khue Post a comment

When developing apps with Swift, there might be cases where you need to merge two dictionaries, such as:

  • Combining default settings with custom settings: Suppose you have a default dictionary that contains some default settings, and you want to allow the user to customize some of those settings. In that case, you can create a custom dictionary that contains the user’s settings and then merge it with the default dictionary.
  • Combining data from multiple sources: If you have multiple dictionaries that contain different pieces of data, you may need to combine them into a single dictionary, such as a dictionary that contains the information of a user and another dictionary that contains his or her preferences.

To merge two dictionaries, you can use the dictionary.merge() method or the dictionary.merging() method. We’ll see more great details about them in this practical article.

Using the dictionary.merge() method

This method is used to merge the key-value pairs in a given sequence into the dictionary, using a combining closure to determine the value for any duplicate keys. The method takes two parameters: a sequence of key-value pairs and a closure that takes the current and new values for any duplicate keys. The method mutates the original dictionary and returns nothing. 

In the following example, the dictionary.merge() method updates a dictionary named dict1 with the key-value pairs from another dictionary called dict2. We will use the current values (of dict1) for any duplicate keys.

var dict1 = [
    "key1": "value1",
    "key2": "value2",
    "key3": "value3",
]

let dict2 = [
    "key2": "value2 of dict2",
    "key3": "value3 of dict2",
    "key4": "value4",
    "key5": "value5",
    "key6": "value6",
]

dict1.merge(dict2) { current, _ in current }
print(dict1)

Output:

[
  "key1": "value1", 
  "key6": "value6", 
  "key2": "value2", 
  "key4": "value4", 
  "key3": "value3", 
  "key5": "value5"
]

If you want to use values from dict2 for any duplicate keys, you can call the merge() method like this:

dict1.merge(dict2) { (_, new) in new }

The output will be:

[
  "key1": "value1", 
  "key3": "value3 of dict2", 
  "key2": "value2 of dict2", 
  "key5": "value5", 
  "key6": "value6", 
  "key4": "value4"
]

Using the dictionary.merging() method

This method works almost the same as the dictionary.merge() method, except it returns a new array instead of mutating the original array.

Example:

let dict1 = [
    "key1": "value1",
    "key2": "value2",
    "key3": "value3",
]

let dict2 = [
    "key2": "value2 of dict2",
    "key3": "value3 of dict2",
    "key4": "value4",
    "key5": "value5",
    "key6": "value6",
]

// Using the current value for any duplicate keys
let dict3 = dict1.merging(dict2) { current, _ in current }
print(dict3)

// Using the new value for any duplicate keys
let dict4 = dict1.merging(dict2) { (_, new) in new }
print(dict4)

You can run the code and see the output on your own. Happy coding & have a nice day!