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

Dawson Young
Dawson Young
512 Points

Stuck

I took a week off from learning which I should not have done now i'm slumped. Not sure where to begin on this code challenge and definitely not sure where to finish

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

    // End code
}

1 Answer

Chase Marchione
Chase Marchione
155,055 Points

Hi Dawson,

Per the instructions: "To do this you're going to use a switch statement and switch on the key. For cases where the key is a European country, append the value (not the key!) to the europeanCapitals array. For keys that are Asian countries, append the value to asianCapitals and finally for the default case, append the values to otherCapitals."

  • We're going to write a switch statement that executes based on what continent the key from the dictionary is from. How exactly the switch statement will execute depends on if the capital is European, Asian, or of a different continent.

  • You might consider the 'case' part of a switch statement as if it is saying 'in case this is true'. So, we're telling the compiler what should happen in the case that it's true that the capital is European, Asian, or of a different continent. Thus, we need 3 cases to account for those 3 different possibilities.

  • How can we write the logic for checking if the capital is European, Asian, or of another continent? Well, we know that Brussels, Vaduz, and Sofia are European. New Delhi and Hanoi are Asian. As for any other capitals, we can just make the case statement go to a default clause, instead of listing all of the non-European and non-Asian capitals.

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
}

Hope this helps!