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

Bruce Röttgers
Bruce Röttgers
18,211 Points

Hey, I don't know how to solve, can you help me? (Question #1)

I think I'm doing false at the case, but don't get out, how it's right.

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 world {
    case world["BEL"]: europeanCapitals.append(world["BEL"])
    case "LIE": europeanCapitals.append(world["LIE"])
    case "BGR": europeanCapitals.append(world["BGR"])
    case "USA": otherCapitals.append(world["USA"])
    case "MEX": otherCapitals.append(world["MEX"])
    case "BRA": otherCapitals.append(world["BRA"])
    case "IND": asianCapitals.append(world["IND"])
    case "VNM": asianCapitals.append(world["VNM"])
    default: print("ERROR: 404")
    }
    // End code
}

1 Answer

David Lin
David Lin
35,864 Points

You're switching on world, which is the enitre dictionary. Rather, switch on key, which is a specific key in the dictionary you're looping through. Then add the value to the appropriate array.

For example:

for (key, value) in world {
    // Enter your code below
    switch key {
    case "BEL": europeanCapitals.append(value)
      ... etc ...
    }
    // End code

Also, to help reduce redundant code, you can include many different cases in one case statement, if you want to operate on all of those cases with the same operation.

For example:

for (key, value) in world {
    // Enter your code below
    switch key {
    case "BEL", "LIE", "BGR": europeanCapitals.append(value)
      ... etc ...
    }
    // End code