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

Michael Cafin
seal-mask
.a{fill-rule:evenodd;}techdegree
Michael Cafin
iOS Development Techdegree Student 1,221 Points

In need of some pointers

Hi,

This is what I've got so far and understand it is in need of adjustments. Please, advise on the following:

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

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

3 Answers

Dhanish Gajjar
Dhanish Gajjar
20,185 Points

When you define for(key, value) in world you have 2 constants, key and value respectively.

So in your switch statement, you only switch on the key constant and append the value to the corresponding Arrays. Like so ..

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", "LIE", "BGR": europeanCapitals.append(value)
    case "VNM", "IND": asianCapitals.append(value)
    default : otherCapitals.append(value)
    }
    // End code
}

Hope it helps.

Michael Cafin
seal-mask
.a{fill-rule:evenodd;}techdegree
Michael Cafin
iOS Development Techdegree Student 1,221 Points

Thank you Dhanish it is much appreciated. I was not aware that it could be that easy with a dictionary - I was overthinking

Dhanish Gajjar
Dhanish Gajjar
20,185 Points

No problem! Glad it helped. I always recommend seeing the videos again, just listening carefully and taking notes will help a lot. Taking slow is the best way.

Michael Cafin
seal-mask
.a{fill-rule:evenodd;}techdegree
Michael Cafin
iOS Development Techdegree Student 1,221 Points

Thx for the tips Dhanish. Glad you confirm most of my study techniques.

I definitely will see the videos again which is something I have not done yet (and perhaps update my notes!)