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

Alex Deltomme
Alex Deltomme
1,628 Points

My Code could not be compiled... What am I doing wrong?

Goal is to iterate through the dictionary and end up with just the names of the capital cities in the relevant way. What am I doing wrong?

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 "Brussels", "Vaduz", "Sofia" : europeanCapitals.append
    case "New Delhi", "Hanoi" : asianCapitals.append  
    default: otherCapitals.append

    // End code
}

1 Answer

Hi Alex Deltomme,

The for loop gives you a key, value pair from the dictionary called world, you can use these in your switch statement to append the value of each key to the right list. You are not using either key or value in your code.

First off this is not the the way to append something to a list:

europeanCapitals.append

Append always takes an argument between parentheses which it should add to the list. So this should be:

europeanCapitals.append(value)

Second you are switching over the wrong thing:

switch world

You need to apply the switch to the key values within world, not the list itself:

switch(key){
case "BEL", "LIE", "BGR": europeanCapitals.append(value)
//etc.
}

I hope this helped!


sig

Alex Deltomme
Alex Deltomme
1,628 Points

this worked, thank you!