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

Paul Je
Paul Je
4,435 Points

Stuck again!

All seems right, but all is wrong (my life's mantra) haha. Was wondering if somebody can help out on what I need to change to allow this code, thanks!

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

4 Answers

Hi Paul,

You're doing great there - but you want to switch on the key only - not the value as well.

Then you want to add the value to the location-specific array if the key is matched in the switch.

for (key) in world {  //<- just the key
    // Enter your code below

    switch (key) {
    case "BEL", "LIE", "BGR": europeanCapitals.append(value) // <- add to the specific array
    case "IND", "VNM": asianCapitals.append(value)
    default:  otherCapitals.append(value)
    // End code
  }

Make sense? If not; shout ...

Steve.

Paul Je
Paul Je
4,435 Points

Makes so much more sense Steve.. #blessup

No problem - I hope you understood the points covered.

Steve.

Paul Je
Paul Je
4,435 Points

Actually I've tried posting it with that recommendation and it doesn't work..

That's odd, it worked here. What error does it throw and can you paste your whole code in, please?

Steve.

Argh! The challenge won't work on a mobile. The ridiculously long question won't get out of the way and I can't reach the submit button at all. Annoying.

Paul Je
Paul Je
4,435 Points

for (key, value) in world { // <-- Keep the entire key,value format (similar to singular "airplaneCode" example in video) // Enter your code below

switch (key) {  // (TURN ON THE KEY ONLY HERE!)
case "BEL", "LIE", "BGR": europeanCapitals.append(value) // <- add to the specific array
case "IND", "VNM": asianCapitals.append(value)
default:  otherCapitals.append(value)
// End code

} }

This is kinda rough, but basically you keep the key, value in the beginning, and switch it on in the line after. #thelittlethings and haha that sucks ... hopefully that can get fixed !!

Sorry - I got that wrong on my phone. Oops!

As in my answer, you want to switch on the key only. The value needs to be available within the for loop because you need to use it!