Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Swift Collections and Control Flow Control Flow With Conditional Statements Working With Switch Statements

Ingunn Augdal Fløvig
Ingunn Augdal Fløvig
1,507 Points

Error 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.

operators.swift
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
seal-mask
.a{fill-rule:evenodd;}techdegree
Rogier Nitschelm
iOS Development Techdegree Student 5,461 Points

Your 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...
}