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

Erol Bickici
Erol Bickici
4,392 Points

Switches and Cases issue

I honestly have no idea what the issue is but I'm having trouble retrieving data from the dictionary of world and putting it in the switch statement. If anyone could help with this, it would be really helpful. Thanks

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(value)
      case ["LIE"] : europeanCapitals.append(value)
      case ["BGR"]: europeanCapitals.append(value)
      case ["IND"]: asianCapitals.append(value)
      case ["VNM"]: asianCapitals.append(value)
      default: otherCapitals.append(value)
      }
    // End code
}

1 Answer

andren
andren
28,558 Points

There are two issues with your code:

  1. You are switching on world, which is the dictionary itself rather thankey which is the variable that will hold the current key that is being processed by the loop.

  2. You have placed square brackets around the cases which is incorrect. The key will just be a normal string so you don't need to have the cases be anything but standard strings either.

If you fix those two issues like this:

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 { // Changed world to key
      case "BEL" : europeanCapitals.append(value) // Removed [] brackets
      case "LIE" : europeanCapitals.append(value) // Removed [] brackets
      case "BGR": europeanCapitals.append(value) // Removed [] brackets
      case "IND": asianCapitals.append(value) // Removed [] brackets
      case "VNM": asianCapitals.append(value) // Removed [] brackets
      default: otherCapitals.append(value)
      }
    // End code
}

Then your code will work.

Erol Bickici
Erol Bickici
4,392 Points

ok thank you very much!