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

Milos Ribera
Milos Ribera
9,633 Points

Using switch to extrapolate values from a dictionary and putting it into an array

I'm stuck, and it's so annoying.

I commented every logical thing I had and why I wrote it.

Can you help me, and can you also help me to understand my logical errors?

Thank you so much!

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 { // I don't know if (key, value) it's a way to rapresente each item is in the dictonary
    // Enter your code below
    switch (key, value) { // I took this statment thinking to get the elements in a dictonary, so later I decide to get the key 
    case "BEL": europeanCapitals.append(value) // "in case" it's BEL -> append to europeanCapitals the relative value (I'm not sure about it)
    case "BGR": europeanCapitals.append("BGR") // here I try to append the value of the key in the dictonary
    case "USA": otherCapitals.append(world["USA"]) // here I tought, in case of key USA, append the value of the dictonary 
    case MEX: otherCapitals.append("MEX") // here I thought I heve to identify the key of a dictonary without " "
    case "BRA": otherCapitals.append(world("BRA")) // ...using other syntax of above
    case key: asianCapitals.append(value) // thinking the loop going to use each  items of the dictoanry
    case "VNM": asianCapitals = asianCapitals + [world(VNM)] // probably other metod to append items to the array (I think it's wrong
    case BEL: europeanCapitals.append("BEL") // no more trying :)
    default: nil // I think I can also get it out, but how set the default it didn't express 
    }
    // End code
}

2 Answers

Jonathan Ruiz
Jonathan Ruiz
2,998 Points

Hi Milos I think for this challenge you over thought it. The problem has a dictionary and they have two parts the key and the value. When writing a switch statement you only have one thing you will switch on. In this case we have a dictionary and we want to switch on the key and append the value to one of the empty arrays.

switch key { 
   case "BEL", "LIE", "BGR": europeanCapitals.append(value) 
  // we switch on the key, for each case we put the keys we are switching then : now we put the empty array we want to append to. The thing we append is this keys value so we append the value
}

This is the first case but I think it should help you solve it just follow this approach.

Milos Ribera
Milos Ribera
9,633 Points

Thank you so much! Everything clear :)