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

Help on 1st challenge question of combining switch statements and ranges, etc.

I am stuck on the first challenge question of control flow for swift.

I am not sure about the following things:

  1. Syntax/ format is correct
  2. Where would I use the constant, let World?
  3. for the asianCapitals, how would I include that in the switch statement.
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
    for europeanCapital in

    europeanCapitals {
    switch europeanCapital {
    case "BEL": 
    print("Brussels")
    case "BGR":
    print("Sofia")
    case 
    default: 

    // End code
}

1 Answer

Tobias Helmrich
Tobias Helmrich
31,602 Points

Hey there,

you don't have to write a second loop inside of the existing loop and note that the europeanCapitals array is empty in the beginning because you have to fill it so the loop wouldn't even execute.

To solve this challenge you just need a switch statement with all the different cases and note that you can group cases by separating them with a comma. In the switch statement you only need two cases (for european capitals and asian capitals) and one default case (for other capitals).

In the cases you shouldn't print out the city names but add them to the appropriate array (europeanCapitals, asianCapitals or otherCapitals) and also take care that you don't hardcode the cities but that you use the value constant you get when you loop over the world dictionary.

Like so:

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", "LIE", "BGR":
        europeanCapitals.append(value)
    case "IND", "VNM":
        asianCapitals.append(value)
    default:
        otherCapitals.append(value)
    }
    // End code
}

I hope that helps, if I didn't answer all of your questions or if you have further questions feel free to ask! :)

Hi Tobias,

Sorry for the delay in response. Thank you very much and yes you answered my questions well!