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 trialEddie Aguilar
2,393 Pointscannot move past this
Cannot seem to figure out what is wrong, can some please explain what is not written correctly?
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 "LIE","BEL", "BGR" : europeanCapitals.append(value)
case "VNM" : asianCapitals.append(value)
case "USA", "MEX", "BRA", "IND" : otherCapitals.append(value)
}
// End code
}
1 Answer
Greg Kaleka
39,021 PointsHi Eddie,
Note that when you submit your code it tells you it could not be compiled. If you click on the Preview button, you'll see this error:
error: switch must be exhaustive, consider adding a default clause
In Swift, you must cover every possible case with a switch statement. We need a default case, since we're comparing strings (note if you were checking an enum
, Swift would know if you'd covered all options). We actually don't have to change much in your code - we can simply check for European capitals, Asian capitals, and if neither of those match, default to Other capitals:
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 "LIE","BEL", "BGR" : europeanCapitals.append(value)
case "VNM" : asianCapitals.append(value)
default : otherCapitals.append(value)
}
// End code
}
Hopefully that makes sense!
Cheers
-Greg