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 trialIngunn Augdal Fløvig
1,507 PointsError mismatch between String and [String: String] in case statement in switch code
I got stuck on this after trying different ways to pull out the information from the dictionary into the case part. The error said that one part is a String and the other [String:String], but I thought that when I write world["BEL"], this should only give out the string "Brussels", so I'm confused about which parts don't match.
Also, I tried to put all the remaining capitals into one switch statement, but can't figure out what to put to avoid having to write out the code for each capital. I tried
default: otherCapitals.append(world[value])
because I thought this would put only the value from the dictionary from whichever other capital there was, but this only gave me an error.
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 world {
case world["BEL"]: europeanCapitals.append("Brussels")
case "LIE": europeanCapitals.append("Vaduz")
case "BGR": europeanCapitals.append("Sofia")
case "VNM": asianCapitals.append("Hanoi")
default: otherCapitals.append("IDK")
}
}
2 Answers
Rogier Nitschelm
iOS Development Techdegree Student 5,461 PointsYour switch-statement is not yet correct. You are passing world
to the switch, but world
is a dictionary.
What you want to do is pass the key to the switch. So it checks that key and if it matches, appends the value.
Like so:
switch key {
case "BEL": europeanCapitals.append("Brussels")
...snip...
}
Ingunn Augdal Fløvig
1,507 PointsAha, thanks, got it!