8

Swift gives us plenty new abilities like (at last!) concatenating strings and even arrays. But no support for dictionaries. Is the only way to concatenate dictionaries is to overload + operation for them?

let string = "Hello" + "World" // "HelloWorld"
let array = ["Hello"] + ["World"] // ["Hello", "World"]
let dict = ["1" : "Hello"] + ["2" : "World"] // error =(
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
katleta3000
  • 2,484
  • 1
  • 18
  • 23
  • see this link may be help with you http://stackoverflow.com/questions/30948326/how-to-combine-two-nsdictionary-in-swift – Anbu.Karthik Oct 15 '15 at 12:53
  • 2
    check this too...http://stackoverflow.com/questions/24051904/how-do-you-add-a-dictionary-of-items-into-another-dictionary – Bhavin Bhadani Oct 15 '15 at 12:53

4 Answers4

18

Use it like this:

  1. Put this anywhere, e.g. Dictionary+Extension.swift:

     func +<Key, Value> (lhs: [Key: Value], rhs: [Key: Value]) -> [Key: Value] {
         var result = lhs
         rhs.forEach{ result[$0] = $1 }
         return result
     }
    
  2. Now your code just work

     let string = "Hello" + "World" // "HelloWorld"
     let array = ["Hello"] + ["World"] // ["Hello", "World"]
     let dict = ["1" : "Hello"] + ["2" : "World"] // okay =)
    
Yuchen
  • 30,852
  • 26
  • 164
  • 234
  • `+` is a bad choice: it's usually a commutative operator, which this one here is not. – Raphael Apr 10 '17 at 15:09
  • Hey @Raphael, why is it not commutative? Given two dictionary, you'll get same result with either `["1" : "Hello"] + ["2" : "World"]` or `["2" : "World"] + ["1" : "Hello"]` – Yuchen Apr 10 '17 at 15:44
  • 1
    @YuchenZhong Consider the case of conflicting keys: `[1: 2, 2: 3] + [2: 4, 3: 5]` vs `[2: 4, 3: 5] + [1: 2, 2: 3]`. – Raphael Apr 11 '17 at 09:26
  • Hmm, @Raphael, that's a very good call! Yay, in this case, the `+` is somewhat misleading. – Yuchen Apr 11 '17 at 12:18
  • 2
    String concatenation (`+`) isn't commutative anyway. – chan1142 Jun 16 '17 at 22:45
  • Should be static for Swift 3 – Joe Jun 21 '17 at 17:04
  • @JosephK sorry I missed your comment. Could you elaborate? What makes you think it needs to be static for Swift 3. I tried it under both Swift 3 and 4, and it seems fine. – Yuchen Dec 08 '17 at 13:10
12

That is not possible because there can be matching keys in the second dictionary. But you can do it manually and the values in the dictionary will be replaced in that case.

var dict = ["1" : "Hello"]
let dict2 = ["2" : "World"]

for key in dict2.keys {
    dict[key] = dict2[key]
}
Adam
  • 26,549
  • 8
  • 62
  • 79
3

You also can use Swift provided functions to do the merge:

public func +<K, V>(left: [K:V], right: [K:V]) -> [K:V] {
    return left.merging(right) { $1 }
}

$1 will take common keys from right dictionary, you can use $0 if you want to give priority to left dictionay.

0

This can be done by using for each loop and inout keyword

func mergDict(firstDict:inout [String:Any],  secondDict:[String:Any]){
    secondDict.forEach { (key, value) in
        firstDict[key] = value
    }
}

than call like

mergDict(firstDict: &firstDictToMerge, secondDict: secondDictToMerge)

you will see that your firstDictToMerge will be merged with second one without using any other variable

Mohit Kumar
  • 9
  • 1
  • 2
  • Theres no need to use `forEach`. In fact there's almost never a need to use `forEach`. It just makes your code look ugly. Use a for-loop instead. – Peter Schorn Aug 18 '20 at 02:54