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

How to use switch with a dictionary

I think I understand from the video pretty well how to use switch when using a regular array. But the quiz at the end uses a dictionary and I am really struggling. I know the bellow code is very very wrong and I could use some help. The goal is to get europeanCapitals to have the value for all of the European key values listed in world. Same for asian and other. All help is appreciated.

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 {

// Where my code begins//

switch (key, value) {
case "BEL": "Brussels": europeanCapitals.append(world["BEL"])

//tried something slightly different for the first case// case "LIE": "Vaduz": europeanCapitals.append("Vaduz") case "BGR": "Sofia": europeanCapitals.append("Sofia") case "USA": "Washington D.C." case "MEX": "Mexico City" case "BRA": "Brasilia" case "IND": "New Delhi": asianCapitals: [String] = ["New Delhi"] case "VNM": "Hanoi": asianCapitals: [String] = ["Hanoi"] defaul: print("other capitals") } }

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

Michael Reining
Michael Reining
10,101 Points

Hi David,

The switch statement for a dictionary is the same as for an array.
You just use the key from the dictionary to switch 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 {
    // Enter your code below
    switch key { // switch the key
case "LIE", "BGR", "BEL": // enter key values
    europeanCapitals.append(value)
case "IND", "VNM": // entery key values
    asianCapitals.append(value)
default:
    otherCapitals.append(value)
}
    // End code
}

I hope that helps,

Mike

PS: Thanks to the awesome resources on Team Treehouse, I launched my first app.

Now you can practice writing Swift code directly on your iPhone :-)

Code! Learn how to program with Swift

Thanks for the help! I'll be sure to download your app once I make the change from android to iphone.

iffat maqbool
iffat maqbool
1,919 Points

Hi Mike, I tried your Swift Learning app. I like the black look of the tableView. Thank you for making it. How long did it take you to make it?

Michael Reining
Michael Reining
10,101 Points

Well, it took me a year to just learn swift and build smaller apps before I tried to build this particular app. I have a full-time job so just doing it in my spare time on nights and weekends. Thanks for taking the time to check out the app. :-)

iffat maqbool
iffat maqbool
1,919 Points

Hi Michael, is there a way to make a tabBar item perform a function (for example play audio) and not move to a different scene? Sorry for asking in the comment, I'm not sure how to ask you directly in a new message.

Michael Reining
Michael Reining
10,101 Points

Of course there is. In InterfaceBuilder you add the action such as buttonPressed which will fire on Touch Up Inside. Inside the buttonPressed method you can run any code you like. It does not have to open up a new scene / view. I hope that helps.