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

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,602 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! :)