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 2.0 Collections and Control Flow Control Flow With Conditional Statements Working with Switch Statements

Stuck

Here we have a dictionary of type [String: String] that contains a three letter country code as a key and that country's capital city as the associated value.

We also have three empty arrays, europeanCapitals, asianCapitals, and otherCapitals. The goal is to iterate through the dictionary and end up with just the names of the capital cities in the relevant array.

For example, after you execute the code you write, europeanCapitals will have the values ["Vaduz", "Brussels", "Sofia"] (not necessarily in that order).

To do this you're going to use a switch statement and switch on the key. For cases where the key is a European country, append the value (not the key!) to the europeanCapitals array. For keys that are Asian countries, append the value to asianCapitals and finally for the default case, append the values to otherCapitals.

  • Tried with or without so many curly braces... if my problem is with "print" I'm not sure how i would write that differently
switch.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 {
    case "BEL": print("Brussels")
    case "LIE": print("Vaduz")
    case "BGR": print("Sofia")
    default: europeanCapitals.append(world)
    }
    switch {
    case "USA": print("Washington D.C.")
    case "MEX": print("Mexico City")
    case "BRA": print("Brasilia")
    default: otherCapitals.append(world)
    }
    switch {
    case "IND": print("New Delhi")
    case "VNM": print("Hanoi")
    default: asainCapitals.append(world)
    }
    // End code
}

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there Josef Dosch ! I think you're probably doing very well, but I also suspect you're overthinking this. The code needed is actually much shorter than what you have here. So I'm going to show you what I have, and then I'll walk you through it:

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
}

Now, we are given three empty arrays to start with. One will contain European capitals, one will contain asian capitals, and the last will contain other capitals. The thing is, we only want the names of the cities stored in those arrays. For example we don't want "USA" stored in otherCapitals. We want "Washington D.C." stored in that array. So we're going to go down a list of abbreviations. The abbreviations are the keys and the city names are the values. There is no print needed. When get an abbreviation we look it up in our switch list. Remember, the abbreviation is the key and we're "switching" on the key. So if it's BEL, LIE, or BGR we're going to look up that abbreviation and then put the value at that abbreviation (the city name) into the europeanCapitals array. And so forth.

Hope this helps! Happy coding! :sparkles:

yea wow defiantly overkill. thank you.

Tyron Spencer
Tyron Spencer
1,489 Points

Hi, Jennifer thankyou for that, it helped me too