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

Khaled M
Khaled M
829 Points

I'm stuck on this question. The format is rather different from the examples in the videos.

I don't understand this new format for the for loop

for (key, value) in world

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
  switch world {
    case "BEL", "LIE", "BGR": europeanCapitals.append("BEL")
        case "VNM", "IND": asianCapitals.append("VNM")
        case "USA", "MEX", "BRA": otherCapitals.append("USA")
  }
    // End code
}

1 Answer

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,526 Points

Hi Kaled,

You are SUPER close and almost have it. You just need to fix up a couple of things.

In the world dictionary, there are two separate values. One is the key and the other is the value. Here the Country Code is the key and the Country name is the value. That's why the for statement is like that. It is going to loop over each key and value pair inside the world dictionary.

With that in mind:

  1. For the switch statement, you need to switch on what is being stored in the key variable. Right now, it's trying to "switch" on the entire dictionary.

  2. As the loop goes through every key, value pair (now that it's switching on the key), with each case, you will want to append what is being stored in the value variable to the new array.

  3. Switch statements should be "exhaustive", meaning there should always be a default case. The instructions hint at this by stating: "for the default case, append the values to otherCapitals.

With the exception of the missing default case, your syntax and logic is spot on! I hope this helps to clear up the bit of confusion you had with the syntax of the for loop. Have a look at the corrected syntax and then take another read of the last paragraph of the challenge's instructions. I'm sure when you see them together, it'll make much more sense. :)

for (key, value) in world {
    // Enter your code below
  switch key {
    case "BEL", "LIE", "BGR": europeanCapitals.append(value)
    case "VNM", "IND": asianCapitals.append(value)
    default: otherCapitals.append(value)
  }
    // End code

Nice work!! :) :dizzy: