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

Miguel Correia
Miguel Correia
1,216 Points

Need some explanation

I need some explanation in this one guys, please don't tell me the answer just explain it to me because I have been in this exercise for over an hour and I really want to go thought without cheating. Thank you guys.

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

    // End code
}

2 Answers

Jhoan Arango
Jhoan Arango
14,575 Points

Hello:

So all you have to do is a switch, that will switch to key, and on each case you will then append value to it. Make sure that the default value is one of them.

switch _ {

case "" :
// Do something

case "" :
// Do something

default: 
// Do whatever was left
}

This will guide you a bit to your answer. Hope it helps

If you need more help please let me know. I admire then fact that you want to get the answer on your own without "cheating".

Good luck

Miguel Correia
Miguel Correia
1,216 Points

This is what I came with, is any of this ones right ?

<p>
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 europeanCapitals {
  case "BEL": print("Brussels")
  case "LIE": print("Vaduz")
  case "BGR": print("Sofia")
  default: print("Nothing else")
}
switch asianCapitals {
  case "IND": print("New Delhi")
  case "VNM": print("Hanoi")
  default: print("Nothing else")
}
switch otherCapitals {
  case "USA": print("Washigton D.C")
  case "MEX": print("Mexico City")
  case "BRA": print("Brasilia")
  default: print("Nothing else")
}
    // End code
}

Or this one

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, value) {

    case europeanCapitals : print("Brussels", "Vaduz", "Sofia")
        // Do something

    case asianCapitals : print("New Delhi", "Hanoi")

        // Do something

    default : print("Washington D.C.", "Mexico City", "Brasilia")

        // Do whatever was left
}
Jhoan Arango
Jhoan Arango
14,575 Points

Hey Miguel:

Well, you are close, you are printing instead of appending the values to the arrays. Also you do not switch on the capital arrays, you have to switch on key then append value to these arrays.

The for in loop is iterating through the dictionary world, and giving you back each key and value, so when you switch on key, you will get in return a value. This value needs to be added to the corresponding array.

for (key, value) in world {
    // Enter your code below
    switch key {
    case "BEL","LIE", "BGR":
        europeanCapitals.append(value)
    case "USA","MEX","BRA" :
        otherCapitals.append(value)
    default: 
       asianCapitals.append(value)
    }
    // End code
}

I'm not sure if you are doing these challenges on your Xcode, but if not, then open a playground and do them there, you will get better results. Then you can copy and paste your code and pass your challenges.

Good luck

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 { case "BEL": europeanCapitals.append("Brussels") case "LIE": europeanCapitals.append("Vaduz") case "BGR": europeanCapitals.append("Sofia") case "USA": otherCapitals.append("Washington D.C.") case "MEX": otherCapitals.append("Mexico City") case "BRA": otherCapitals.append("Brasilia") case "IND": asianCapitals.append("New Delhi") case "VMN": asianCapitals.append("Hanoi") default: asianCapitals.append(value) } // End code }