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

Raymond Espinoza
Raymond Espinoza
1,989 Points

switch statements

code wont work got stuck and not to sure if default code is right ?

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 world {
    case europeanCapitals : print("Vaduz","Sofia","Brussels")
    case asianCapitals : print("Brasilia","New Delhi","Hanoi",)
    default otherCapitals : print("Washington D.C","Mexico City")
    }

    // End code
}

1 Answer

Steven Deutsch
Steven Deutsch
21,046 Points

Hey Raymond Espinoza,

There's a few things that we need to fix in your code. First, you need to switch on the key from the (key, value) tuple for world. Not world itself. Second, inside your switch statement, you need to write cases for conditional code to execute based on what key is matched. If the key passed into your switch statement matches a case, the code for that case will be executed. So, when you match a certain key - you want to write code that will append the corresponding value to its appropriate array.

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 on key values from (key, value) for world
    switch key {
    // if key == BEL, LIE, or BGR, append value to europeanCapitals array
    case "BEL", "LIE", "BGR": europeanCapitals.append(value)
    // if key == IND or VNM, append value to asianCapitals array
    case "IND", "VNM": asianCapitals.append(value)
    // append values from all other keys to otherCapitals array
    default: otherCapitals.append(value)
    }

    // End code
}

Hope this helps! Good Luck!