Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,845 questions

51,766 answers

573 users

How to combine 2 maps into a third map in Swift

2 Answers

0 votes
import Foundation

let map1 = ["a": 1, "b": 2]
let map2 = ["b": 999, "c": 4, "d": 5]

let combined = map1.merging(map2) { (_, new) in new }

print(combined)




/*
run:

["c": 4, "b": 999, "a": 1, "d": 5]

*/

 



answered Aug 26, 2025 by avibootz
0 votes
import Foundation

let map1 = ["a": 1, "b": 2]
let map2 = ["b": 999, "c": 4, "d": 5]

let combined = map1.merging(map2) { old, new in old + new }

print(combined)




/*
run:

["d": 5, "c": 4, "a": 1, "b": 1001]

*/

 



answered Aug 26, 2025 by avibootz

Related questions

1 answer 61 views
2 answers 56 views
3 answers 80 views
2 answers 81 views
1 answer 58 views
...