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

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
231,275 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
231,275 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!