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

Having compiler errors. I'm completely lost

Having a few problems with this challenge.

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": europeanCapitals.append("Brussels")
    case "LIE": europeanCapitals.append("Vaduz")
    case "BGR": europeanCapitals.append("Sofia")
    case "IND": asianCapitals.append("New Delhi")
    case "VNM": asianCapitals.append("Hanoi")
    default: otherCapitals.append()
    }
    // End code
}

1 Answer

andren
andren
28,502 Points

Your code is not too far off. The main issue is that you switch on the world array itself, rather than the key of the array and that you aren't filling in a value for the otherCapitals append method.

The for loop that the switch statement is inside of takes the world array and separates it into two variables key and value. During the first loop key equals the first key of the array (BEL) and value the first value (Brussels), the second loop you get the second key (LIE) and second value (Vaduz) and so on.

Therefore you can use key within the switch to only compare things to the key, and you can also use value to insert whatever value is currently being looped though.

So if you simply switch on the key and append value to the otherCapitals array like this:

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

Then your code will work. Though It's worth pointing out that the code can be simplified a bit. Since you can use value to insert whatever value is being looped through you can condense the switch into only 3 case checks. One that checks for European Capitals, one for Asian Capitals and one for other. 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)
      default: otherCapitals.append(value)
    }
    // End code
}