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 2.0 Collections and Control Flow Control Flow With Conditional Statements Working with Switch Statements

Problem with the code

I would like to know if I'm making some kind of error within my code. I found a similar question to my initial question how how to solve the question and learned key was my mistake since we are looking for the keys within world then appending the values into the correct array. I tried typing it myself twice and even copied and pasted the code. The continued error I get is: remember to append to the correct arrays.

If someone sees something missing within the code please let me know and explain so I can get a better understanding. Thank you for all your help

switch.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 in world {
    // Enter your code below
    switch {
    case "BEL", "BGR", "LIE": europeanCapitals.append(value)
    case "IND", "VNM": asianCapitals.append(value)
    default: otherCapitals.append(value)
    }
    // End code
}

Actually I think I figured out my error. I didn't mentioned key after I wrote switch. WIll try it now.

Jhoan Arango
Jhoan Arango
14,575 Points

Did you figure it out ?

Jhoan Arango Yes I did. Thank you for checking up on my post. As I mentioned before, I was missing Key within my code.

1 Answer

jonlunsford
jonlunsford
15,480 Points

You are correct that you need to switch on key. But key will return each key:value pair from the dictionary not just the key. To get the key you will need to setup a tuple like this.

for (key, value) in world {
    switch key { ....

this will allow you to capture both the key and value in separate variables and then switch on the one you want (i.e.; key).