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

Maxwell Catmur
Maxwell Catmur
7,355 Points

how do I append switch statements inside of for loops to arrays when dealing with capital cities?

Please can someone help me with this code challenge? Here is the question:

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.

The code is below: Please can you let me know where I went wrong? 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 (key, value) {
        case "BEL", "Brussels": europeanCapitals.append[]
        case "LIE", "Vaduz": europeanCapitals.append[]
        case "BGR", "Sofia": europeanCapitals.append[]
        case "USA", "Washington D.C": otherCapitals.append[]
        case "MEX", "Mexico City": otherCapitals.append[]
        case "BRA", "Brasilia": otherCapitals.append[]
        case "IND", "New Delhi": asianCapitals.append[]
        case "VNM", "Hanoi": asianCapitals.append[]

    }
    // End code
}

1 Answer

Sarah Hurtgen
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Sarah Hurtgen
Treehouse Project Reviewer

Hi! You're on the right track!

One of the phrases you want to focus on in the directions is "use a switch statement and switch on the key". Your current set up is trying to switch out the key, AND value, with the action of appending. But we need to try and switch only the key out, and then append the value. So for each case, you want to input the key only, and then you can append the corresponding value to the array.

For example:

var realAirports: [String] = []
var fakeAirports: [String] = []

let airports = [
    "LAX": "Los Angeles Airport",
    "YAY": "Funtime Airport"
    "EXE": "Example Airport"
]

for (key, value) in airports {
    switch (key) {
    case "LAX": realAirports.append(value)
    case "YAY": fakeAirports.append(value)
    default: fakeAirports.append(value)
    }
}

Hope that helps!