Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Soccer League Coordinator in Swift

Meedo Al
seal-mask
.a{fill-rule:evenodd;}techdegree
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.