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 Swift 2.0 Collections and Control Flow Control Flow With Conditional Statements Working with Switch Statements

How to solve this challenge?

Can someone please explain how to solve this challenge?

3 Answers

Nathan Tallack
Nathan Tallack
22,158 Points

What you are going to want to do is have a for loop that runs over the dictionry and in that for loop use a switch to work out which category to append the dictionary value to.

In pseudocode, it would look something like this.

for key, value in dictionary {
    switch key {
        case "XXX", "YYY": list1.append(value)
        case "AAA", "BBB": list2.append(value)
        default: otherList.append(value)
    }
}

Here we are using a "switch" to quickly do something that could otherwise be done in a much more time consuming if then else if type manner. :)

I tried this but it's not working

for (key, value) in world { switch key { case "BEL" : europeanCapitals.append(world[0]) } }

Nathan Tallack
Nathan Tallack
22,158 Points

Close. Try something like this.

for (key, value) in world {
    switch key {
        case "BEL", "LIE", "BER": europeanCapitals.append(value)

:)

thanks it worked <3