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

For some reason I don't really understand the concept of switch statements and can't figure this one out....help anyone?

Just looking for a solution to the objective and a quick explanation....thanks!

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
    switch capital {
    case "Brussels".append(E
    // End code
}
Sathya vegunta
Sathya vegunta
4,061 Points

Hi,

Switch is similar to If-else statements, where it goes on checking the conditions till there is a match. Similarly in switch statement we give the condition and case is the options, for Ex: switch condition { case "A" ,"B": print("condition is either A or B") case "C" : print ("conditions C") case "D": print ("Condition is D") default: print("Goes with this statement if the condition in the above statement are not met") } In the above example if the condition value is A or B , then 1st case is executed, if the condition value is C , then the statements under second case is executed etc. if the condition value does not match with any of the cases then the "default" section is executed,

Coming to the Program. we need to check for the Keys and insert the values in the array, so we are passing key to the switch as a condition, and based on the match with the case, the corresponding value is inserted in the array, For Loop iterates and when the Key is "BEL" or "LIE" or "BGR" the 1st case is executed, and when the key is "IND" or "VNM" the 2nd case statements are executed , and for the rest the default clause is executed.

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

1 Answer

You're a little off track, but that's no worry. I had trouble to.

To help you out I've posted my solution, but I strongly suggest going back to the start of switches and giving them a once over. I've also tried to break it down below, see how you go.

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

This challenge is using the "for in" loop method we saw earlier in the course. That loop will check every "key" and its corresponding "value"in the constant called "world". Each time it does that the switch statement will run.

In this case the switch statement needs to check against the value "key". As you will see in my solution above I've set the code as follows,

switch key {
}

Now that the switch knows what to look for being the value stored in "key" in the constant called "world", you can now give it something to compare known as a case.

case "BEL", "LIE", "BGR": europeanCapitals.append(value)

In this example the case is checking for the values BEL, LIE or BGR in the "key". Then after checking that appending the "value" from the "word" constant into the specified array.

Another way to think of it may be as follows.

IN world IF key = BEL, LIE or BGR THEN append the corresponding value to europeanCapitals.

I hope this clears it up.