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 Collections and Control Flow Control Flow With Conditional Statements Working With Switch Statements

I'm stuck on don't even know where to start anymore on this switch challenge.

you need to append the countries capitals to the variable that correspond's with its regions. I'm sure its simple, but I am so lost.

operators.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
}

3 Answers

Nathan F.
Nathan F.
30,773 Points

It's been a while since I tackled this problem, but let's break it down. First of all you'll need to do a little geography refresher. :) We have three major categories: European capitals, Asian capitals, and other. So do a little mental note of which countries are which. For example, we know Mexico (indicated by the dictionary key "MEX") is not an Asian or European capital, so it belongs in 'otherCapitals', right?

From here... We need to do two things. We need to look at our key, and then decide which category it belongs to. If I only cared about finding Mexico in a list such as this, I might write something like:

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
    // check if the key matches Mexico
    if key == "MEX" {
      // we have the dictionary value for Mexico... Let's stash its capital in our variable.
      otherCapitals.append(value)
    }
    // End code
}

If we cared about North American capitals, I might change my code to look like

var northAmericanCapitals: [String] = []
...
for (key, value) in world {
  // if the key matches Mexico OR USA, put it in our variable.
  if key == "MEX" || key == "USA" {
    northAmericanCapitals.append(value)
  }
}

I think you can sort it out from here, but let me know if there's something more you don't understand!

but arent it say need to be switch

Nathan F.
Nathan F.
30,773 Points

Sorry, I misunderstood a bit of it--if you need to use a switch statement, you're going to follow the same general example I've given. Using my first example, but with a switch statement:

for (key, value) in world {
    // Enter your code below
    // check if the key matches Mexico
   switch key {
   case "MEX":
     northAmericanCapitals.append(value)
   default:
     break
   }
    // End code
}