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

Ben Masel
Ben Masel
2,004 Points

Success still eludes me for this code challenge!

I don't really know what I'm doing and i think i need more help to understand switch statements in this code challenge>

switch.swift
var europeanCapitals: [String] = []
var asianCapitals: [String] = []
var otherCapitals: [String] = []

let world = [
  "BEL": "Brussels", 
  "LIE": "Vaduz", 
  "BGR": "Sofia", 
  "USA": "Washington D.C.", 
  "MEX": "Mexico City", 
  "BRA": "Brasilia", 
  "IND": "New Delhi", 
  "VNM": "Hanoi"]

for (key, value) in world {
    // Enter your code below
    switch key{ case "LIE", "BEL", "BGR": europeanCapitals.append(world[key]!)}

    // End code
}

1 Answer

Greg Kaleka
Greg Kaleka
39,021 Points

Hi Benjamin,

First tip: Don't try to write a switch statement on a single line! Proper spacing and indentation makes your code easier to read. It also makes your code easier to write.

Here's the basic structure of a switch statement, properly spaced and indented:

switch.swift
switch somethingThatCouldBeANumberOfThings {
    case theThingIsThis:
        doThisThing()
    case theThingIsThisOtherThing:
        doSomethingElse()
    case theThingIsAnotherFunThing:
        doAFunThingThen()
    default:
        doThisJustInCase()
}

I like to think of switch as meaning "we're going to check the value of...", and case as meaning "if the thing we're checking is..."

So the code you have is actually pretty much good to go. You just need to add the other cases, as per the instructions. Also, you don't need to index into the dictionary - the for in loop is already doing that for you. Just append value.

Give it another go, and report back :smiley:

Cheers :beers:

-Greg