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 trialJos Feseha
4,011 Pointsswitch statement ,I am stucked here please help
how do I do this switch statement ....
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
}
2 Answers
Rogier Nitschelm
iOS Development Techdegree Student 5,461 PointsI made a little error myself, apologies for that.
The otherCapitals would be ideal for a default case, because then we would only have to make the following:
case "BEL": // add all european countries here
europeanCapitals.append(value)
case "IND": // add all asian countries here
asianCapitals.append(value)
default: // the rest automatically goes here
otherCapitals.append(value)
Rogier Nitschelm
iOS Development Techdegree Student 5,461 PointsPerhaps something like this?
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 {
case "BEL":
europeanCapitals.append(value)
case "IND":
asianCapitals.append(value)
default:
break
}
}
Because I do not entirely want to ruin the joy of figuring some additional things out: It is possible to put multiple cases on a single line, so you only need a total of 2 cases plus a default case.
Also, the default case is ideal for appending a capital to an array with capitals too. Which array of capitals would be fit for use as the default case?
Jos Feseha
4,011 PointsTHANK YOU So much ! , I think the asianCapitals will fit for the default case .
Jos Feseha
4,011 PointsJos Feseha
4,011 Pointsohhw ok , Thank You , it worked perfectly !