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

Chris Bahr
Chris Bahr
5,614 Points

Switch statements with dictionaries

For the default statement, I'm not sure how I should append the correct value for the key in the otherCapitals array. Please help if you can!

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 {
      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(world[])
    }
    // End code
}

1 Answer

Jeff McDivitt
Jeff McDivitt
23,970 Points

Hi Chris -

  1. Check your switch statement as you need to append the values to the correct empty array of Capitals. It states for all other Capitals that you append the value to otherCapitals; that is where the default statement comes in.
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 {
    switch key {
    case "BEL","BGR","LIE":
        europeanCapitals.append(value)
    case "IND" :
        asianCapitals.append(value)
    default:
        otherCapitals.append(value)
    }
}
Chris Bahr
Chris Bahr
5,614 Points

Ah I see. So instead of indexing world with the key to append the specific value to the array, I can simply append using 'value'. Thanks!