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

JUNSEI TEI
JUNSEI TEI
2,055 Points

Code Challenge in Collection and Control Flow

Help me! I get stuck here.

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 {

    switch world {
    case "BEL": "Brussels", "LIE": "Vaduz", "BGR": "Sofia" : europeanCapitals.append(value)
    case "IND": "New Delhi", "VNM": "Haoni" : asianCapitals.append(value)
    case "USA": "Washington D.C.", "MEX": "Mexica City", "BRA": "Brasilia" : otherCapitals.append(value)

    }
    // End code
}

2 Answers

Steven Deutsch
Steven Deutsch
21,046 Points

Hey Junsei Tei,

There are a few things we need to correct in your code. First, your switch statement needs to switch on the key value, not world. So you only have to write out the keys in your case statements. Second, you should use a default statement to capture the rest of the remaining keys. This way you don't have to list what those specific keys are. All the values that weren't appended to europeanCapitals or asianCapitals will be appended to otherCapitals by default.

Your code should look like this:

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 {

    switch key {
    case "BEL", "LIE", "BGR": europeanCapitals.append(value)
    case "IND", "VNM": asianCapitals.append(value)
    default: otherCapitals.append(value)

    }
    // End code
}

Hope this helps! Good luck!

JUNSEI TEI
JUNSEI TEI
2,055 Points

Appreciate for your help!!

Emil Rais
Emil Rais
26,873 Points

You're switching on the world dictionary, but that is a dictionary of country codes and capitals. What you're interested in is switching on the country codes. As you iterate through the for loop the country codes will be stored in the constant named key.

JUNSEI TEI
JUNSEI TEI
2,055 Points

Appreciate for your help!