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

walter fernandez
walter fernandez
4,992 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

help please

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

    // End code
}

1 Answer

Hi Walter,

You have to switch on the key of the dictionary inside the loop, and then append the value of the key upon match. The rest of the capitals you can append to the otherCapitals array using the default statement.

For instance:

for (key, value) in world {
    switch key {
        case "KEY","KEY","KEY": europeanCapitals.append(value)
        case "KEY","KEY","KEY": asianCapitals(value)
       default:  otherCapitals.append(value)
     }
}

What you're essentially saying is:

for each key value pair in world switch on the key. In case key is equal to this or this or this string, then append to this array. If the key does not match any of my cases/strings then append to this default array instead.

Happy coding :-)