Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Soccer League Coordinator in Swift

Meedo Al
Meedo Al
iOS Development Techdegree Student 2,203 Points

Merging Two Dictionaries Error

Hello there,

Am trying to proceed with this project, so I've created a dictionary holding each player's information, I've also created a wholistic array filled with each player's dictionary, & then I've created an empty dictionaries for each team to start filling them by players.

however when I try to add to dictionaries to each other, I get compiler error

if I used the notion += between the two dictionaries I get this error:

Binary Operator "+=" cannot be applied to two [String : String] operands

while if I used the function extend or append, I get this error:

value of type '[String : String]' has no member 'extend' or value of type '[String : String]' has no member 'append'

I don't know how to merge two dictionaries or add them to each other?

2 Answers

One suggestion is to Map over the key/values in the first dictionary and add them to the second dictionary. Hope this helps!

var dict1 = ["Name": "Gary", "AnotherName": "Bob"]
var dict2 = ["Website": "TreeHouse"]

dict1.map { key, value in

    dict2[key] = value 

Pavel Fomchenkov
Pavel Fomchenkov
20,897 Points

You aren't supposed to use map, filter, and reduce yet.