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! While you're at it, check out some resources Treehouse students have shared here.

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 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,159 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,159 Points

Close. Try something like this.

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

:)

thanks it worked <3