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 trialcatalinsebastian
9,195 PointsA bit of confusion regarding the challenge up next
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" : asianCapitals.append(value)
default : otherCapitals.append(value)
}
// End code
}
this is my passing code for the challenge after this video , though i've figured it out what it wanted , i am still confused how does switching inside a for loop "(key, value)" in world works.
basically the constants could have been any names right? lets say a and b ,so the "for in" loop iterates over the dictionary assigning its content to a and b.
my question is , how does the for in loop know the b is the value of the key a and pairs them up together and also allowing me to .append(value) getting the propper value. we did not specify something like (and this is some pseudocode i know its not any syntax) : for(( key, value): [String: String]) in {} to let it know
also, can i iterate over a dictionary for only the key or the value ? giving the for in loop only one constant to assign values to ? , if yes how will it know it is the key or the value i need ?
any answer to clear this for me is appreciated , thanks!
1 Answer
Jason Anders
Treehouse Moderator 145,860 PointsHey Catalin,
I'll try my best to explain it.
The array that you have declared has key: value
pairs (eg. "BEL" is the key, and its value is "Brussels"). Now when the for loop
executes, it will go through each key: value
pair one at a time.
Now when you use switch inside of the loop, each iteration of the loop has to run through the switch
before starting the loop again with the next iteration. You will notice that the for loop
is being instructed to loop through "world" and store a value in key
and the corresponding value in value
. Yes, these could have been named anything, but "key" and "value" make sense to us. Regardless of what they are named, the loop sees two variables and will store the key
of the array in the first one and the value
of that particular key in the second one.
Now in the switch statement
, you are switching on the variable "key" which will be holding the key
in each iteration of the loop (see how that makes sense to us to name it that :) ) So, for example, when the first iteration of the loop executes, "key" will be holding "BEL". Now the switch
statement takes over and matches "BEL" to its case
and then executes the .append()
code for that case (appending the value
of "value"). When the switch
has been satisfied, the loop will execute the second iteration, and the "key" now will be holding "LIE", which it passes to the switch
... and so on until every key: value
pair has been iterated through. Once the for loop
is satisfied, that block of code is complete.
I really hope that help to make sense for you. :) Keep Coding!
catalinsebastian
9,195 Pointscatalinsebastian
9,195 Pointsthanks for shedding some light on the matter Jason , i did some research using the docs too meanwhile , i got some understanding of it now .
appreciate taking the time to help, have a nice day!