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 trialBridget Farmer
2,833 PointsI'm stuck on Swift 2.0 Collections and Workflow
I am thrown by the fact that the video case value gave an example of a single name, and the code challenge involves a name with two parts (key, value).
QUESTION: Here we have a dictionary of type [String: String} that contains a three letter country code as a key and that country's capital city as the associated value. We also have three empty arrays, europeanCapitals, asianCapitals and otherCapitals. The goal is to iterate through the dictionary and end up with just the names of the capital cities in the relevant array. For example, after you execute the code you write, europeanCapitals will have the values ["Vaduz", "Brussels", "Sofia"] (not necessarily in that order). To do this you're going to use a switch statement and switch on the key. For cases where the key is a European country, append the value (not the key!) to the europeanCapitals array. For the keys that are Asian countries, append the value to asianCapitals and finally for the default case, append the values to otherCapitals.
CODE: 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 "VNM", "IND": asianCapitals.append("New Delhi", "Hanoi") default: "USA", "MEX", "BRA": otherCapitals.append("Washington D.C.", "Mexico City", "Brasilia") }
8 Answers
Michael Reining
10,101 PointsThe video example iterates through an array called airportCodes.
To iterate through an array you use the format
for item in items...
for airportCode in airportCodes...
etc...
In this code challenge you have to iterate through a dictionary. To iterate through a dictionary you use the format
for (key, value) in dictionary...
for (key, value) in world
if you tried to just use.
for key in world...
then you would have no access to the value which is what you are using in the switch statement. It is the value that we are trying to add / append to the arrays.
switch key {
case "BEL", "LIE", "BGR":
europeanCapitals.append(value)
case "IND","VNM":
asianCapitals.append(value)
default:
otherCapitals.append(value)
}
I hope that helps.
Mike
PS: Thanks to the awesome resources on Team Treehouse, I just launched my first app. :-)
Tobias Mahnert
89,414 PointsHi Bridget,
this Code solves the Challenge.
switch key {
case "BEL", "BGR","LIE" :
europeanCapitals.append(value)
case "IND","VNM":
asianCapitals.append(value)
default:
otherCapitals.append(value)
}
if you want to know why, lease let me know.
Also please mark my answer as best answer if it helped you,
cheers toby
Tobias Mahnert
89,414 PointsHey Morten, the loop where we iterate the world array. Iterate means go through every value in the array (world). In the array every key for example BEL has a value connected to it, in this case Brussels. The key is the identifier and the value the information connected to the identifier. So what the for in loop does is go one step through the array, then next step and again and again. The switch statement is basically like a net which catches special expressions like BEL and if it catches that it gives the command to do something in this case append the connected value to the array. The words key and value have here the role of a variable. It also could have been
var one = "blabla"
var two = "more blablablab"
switch one {
case "blabla": print(two)
default: print("Trump")
}
any questions? Please mark my answer as best answer if it helped you.
Cheers Toby
Tobias Mahnert
89,414 Pointsoh, europeanCapitals is an array to be exactly. so yea it appends the values to it. the default doesn't need any keys because its only there to do something when there is nothing to do for the case statement. And in this code challenge it basically appends the values from the leftover keys in the world array. Does that help?
Morten Larsen
12,275 Pointsyeah, very cool! Put it europeanCapitals just stores our values right?
Tobias Mahnert
89,414 Pointsexactly (:
Morten Larsen
12,275 PointsI need a bit of help understanding this, can someone help me explain why we do:
switch key {}
And then what this last sentence mean:
europeanCapitals.append(value)
Thank you very much.
Morten Larsen
12,275 Pointsand then:
var europeanCapitals is just a string to store our output like, brussels, vagus and sofia?
Is that correct understanding?
Aand by appending that to europeanCapitals.append(value),
What does this exactly do? Does it write the city names out to console?
Teodora Blindu
912 PointsWhat I didn't understand is how do we know that we have to iterate through a dictionary. Why not with a for in loop?