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

syntax error or geographical error?

Bummer! Remember to append the values from each case statement to the correct array.

Here is the code:

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 "BEL": europeanCapitals += ["Brussels"]
    case "LIE": europeanCapitals += ["Vaduz"]
    case "BGR": europeanCapitals += ["Sofia"]
    case "IND": asianCapitals += ["New Delhi"]
    case "VNM": asianCapitals += ["Hanoi"]
    default: otherCapitals += [value]
    }
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 "BEL": europeanCapitals += ["Brussels"]
    case "LIE": europeanCapitals += ["Vaduz"]
    case "BGR": europeanCapitals += ["Sofia"]
    case "IND": asianCapitals += ["New Delhi"]
    case "VNM": asianCapitals += ["Hanoi"]
    default: otherCapitals += [value]
    // End code
}

2 Answers

Ben, interesting. Your code looked good. So just to see what I'd missed I copied it into a playground and it ran fine! It looks like this is an issue with Treehouse. Just as a side comment, you can save a few keystrokes if you combine the cases:

for (key, value) in world {
    // Enter your code below
    switch key {
    case "BEL", "LIE", "BGR": europeanCapitals += [value]
    case "IND", "VNM": asianCapitals += [value]
    default: otherCapitals += [value]
    }
    // End code
}

This gets the same error you do in Treehouse, but runs fine in Xcode. Sometimes it's just an unwanted space in Treehouse, so I tried a few variants, but no go. But Preview now shows no errors, either for your original or my revision.

Tobias Helmrich
Tobias Helmrich
31,603 Points

Hey there,

actually the code should work and you already got an improved version of it. However I tried using the append method instead of the addition assignment operator and then it passed. So I guess that's an error with Treehouse because it only accepts the append method even though the addition assignment operator should be fine as well.

This is working:

for (key, value) in world {
    // Enter your code below
    switch key {
    case "BEL", "LIE", "BGR": europeanCapitals.append(value)
    case "IND", "VNM": asianCapitals.append(value)
    default: otherCapitals.append(value)
    }
    // End code
}

I hope that helps! :)