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

Cannot seem to wrap my mind around this. Can I ask for some assistance?

I have a basic understanding of how the switch statement works but, again, cannot seem to wrap my mind around this one. Can I ask for a descriptive walkthrough?

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

1 Answer

Jonathan Ruiz
Jonathan Ruiz
2,998 Points

Hi Eddie switch statements have a couple parts that are standard. So looking at those when you write one will help you understand what makes a complete switch statement. You must have a thing you are switching on this one is key, if you switch the wrong thing then you won't be doing what you need to be. In this case you are working with a dictionary and they have keys & values. So in this case the key is of type string and that key is what we want to switch on. Now the next thing is the cases, we must write out the keys we want to switch in each case. For this particular example you will be needing multiple keys per case. After that you must append these keys values to one of the empty arrays. The switch statement would look like this for the first case.

for (key, value) in world {
    switch key {
        case "BEL", "LIE", "BGR": europeanCapitals.append(value) 
        // case "key I want to switch", "another key I want to switch": correctArray.append(value) 
       // the value you are appending is the value that goes with the specific key in the dictionary
       // we switch the key "BEL" then append the value to the europeanCapitals empty array 

For your default case they want you to append the remaining keys to the otherCapitals array.