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

Carlos Mendez
Carlos Mendez
2,646 Points

In the editor we have a dictionary that contains a three letter country code as a key and that country's capital city as

What am I doing wrong?? 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, value) { case "BEL", "LIE", "BGR": europeanCapitals.append(value) case "IND", "VNM": asianCapitals.append(value) default: otherCapitals.append(value)

}

// End code

}

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 (key, value) {
    case "BEL", "LIE", "BGR": europeanCapitals.append(value)
    case "IND", "VNM": asianCapitals.append(value)
    default: otherCapitals.append(value)

  }

    // End code
}

Hey Carlos!

You need to change some information in switch statment. You are looking only for a "key" in the world array of dictionary. So you dont need to search for a value. Also the array with otherCapitals are the capitals that not match to Asian or European capitals i.e: "USA", "MEX", "BRA", so your code should look like this:

for (key, value) in world {
    // Enter your code below
    switch key {
    case "BEL", "LIE", "BGR": europeanCapitals.append(value)
    case "IND", "VNM": asianCapitals.append(value)
    case "USA", "MEX", "BRA": otherCapitals.append(value)
    default: print("I don't have this city in my array")
    }

    // End code
}

Let me know if this worked for you :)

Cheers

3 Answers

Steven Parker
Steven Parker
210,108 Points

Your only issue is passing two arguments to "switch" when it expects only one (the key).

Note that while Jakob's code will pass the challenge, it deviates a bit from what the instructions asked for.

Your original code (once fixed) is a more correct solution.

You are right! :) Thanks for that. Default statment in switch could be exaclty like Carlos wrote :)

Cheers!

Carlos Mendez
Carlos Mendez
2,646 Points

That worked! thanks guys! for some reason, anything loop related has been harder to grasp.

Steven Parker
Steven Parker
210,108 Points

Carlos Mendez — Glad to help. You can mark a question solved by choosing a "best answer".
And happy coding!

Thanks for this explanation I was having the hardest time as well. The loop section has been really hard. I've gotten so close and these 1 or two little things have prevented me from fully completing it! It makes a little more sense now. I will go back and read up on this section in the swift notes that Pasan provides!