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

Omer Celik
PLUS
Omer Celik
Courses Plus Student 1,722 Points

Hello, I am kinda stuck here. I have tried couple things but I can't pass it. Could you help me with this please. Thanks

I don't know how to add case result to the array.

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 "LIE", "BEL", "BGR": europeanCapitals.append(world)
    case "VNM", "BRA", "IND": asianCapitals.append(world)
    default: otherCapitals.append(world)

    }
    // End code
}
Omer Celik
Omer Celik
Courses Plus Student 1,722 Points

In the editor we have a dictionary 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.

3 Answers

Charles Kenney
Charles Kenney
15,604 Points

Omer,

In order to map the cities (values) to the correct capitals array you need to switch on the country codes (keys). You made the mistake of switch on the entire object. Otherwise, your code should work well. This was my solution.

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

Hope this helps,

Charles

Omer Celik
Omer Celik
Courses Plus Student 1,722 Points

Thank you very much for quick response I finally got it but I have one more question. Why did you use break after first case?

Charles Kenney
Charles Kenney
15,604 Points

Omer, you are very welcome. As for that break, I honestly don't know why I added it, (it is not necessary) I am just very tired at the moment and made a mistake. Lol, good catch!

Jeff McDivitt
Jeff McDivitt
23,970 Points

Hi Omer -

  1. You are switching on the wrong value, you should switch on the key
  2. Check you case values
  3. You are not appending world you are appending value
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)
    }
}

switch (key) { case "BEL", "BGR", "LIE": europeanCapitals.append(value) case "IND", "VNM": asianCapitals.append(value) default: otherCapitals.append(value) }

this works