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

Ranak Bansal
Ranak Bansal
1,207 Points

Switch Statements code challenge

Hello,

I am not sure what is wrong with my code, did I not append correctly, or is my switch statement just completely wrong? Thank you for your help.

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: world = "BEL"; "LIE"; "BGR" {
      europeanCapitals.append()
      }
      case: world = "IND"; "VNM" {
      asianCapitals.append()
      }
      case: world = "USA"; "BRA"; "MEX" {
      otherCapitals.append()
      }
      default {
      print("I don\'t know what to do") 
      }

    }
    // End code
}

3 Answers

Matthew Long
Matthew Long
28,407 Points

You're off to a decent start! First, you are switching over the key, which you did you just put switch world {} instead of switch key {} in the switch statement. Then you used semicolons instead of commas in your cases. This will cause an error. Then you actually didn't append anything to the capital arrays. Be sure to append the values: .append(value). Finally, the challenge wants you to use the otherCapitals in the world as the default case.

for (key, value) in world {
  switch key {
  case "BEL", "LIE", "BGR": europeanCapitals.append(value)
  case "IND", "VNM": asianCapitals.append(value)
  default: otherCapitals.append(value)
  }
}
Ranak Bansal
Ranak Bansal
1,207 Points

Thanks, Matthew, I just had one more question. Would the values be the cities that correspond to the countries? So would write:

europeanCapitals.append("Brussels", "Vaduz", "Sofia")

asianCapitals.append("New Delhi", "Hanoi")

otherCapitals.append("Washington D.C.", "Brasilia", "Mexico City")

Matthew Long
Matthew Long
28,407 Points

Not really. That is what is being appended, but you don't actually type the cities in. That defeats the purpose of looping over a dictionary a bit. You literally type europeanCapitals.append(value). The value being appended is the value from the key value pair in the dictionary.

Ranak Bansal
Ranak Bansal
1,207 Points

Thanks! I was able to solve it.