-3

Suppose we have this array:

var myArray = ["key":"value","key2":"value2","key3":"value3" ]

how can I update this some keys of this dictionary using another array, myUpdate:

var myUpdate = ["key2":"value2New","key3":"value3New" ]

so the result would be:

var myArray = ["key":"value","key2":"value2New","key3":"value3New" ]
Ali Ashtiani
  • 109
  • 11
  • When reviewing the answers in the duplicate, be sure to look at newer answers using `merge`. – rmaddy Oct 10 '18 at 02:24

1 Answers1

2

Method 1: Loop through the dictionary

for (key,value) in myUpdate
{
    myArray[key] = value
}

Method 2: use merge

myArray.merge(myUpdate, uniquingKeysWith: {$1})
Ricky Mo
  • 5,242
  • 1
  • 12
  • 25
  • Or as a [trailing closure](https://docs.swift.org/swift-book/LanguageGuide/Closures.html#ID102): `myArray.merge(myUpdate) { $1 }` – msbit Oct 10 '18 at 02:40