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 trial

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

Ahmet GULER
Ahmet GULER
7,181 Points

error: value of optional type 'String?' not unwrapped

i am trying the challenge: https://teamtreehouse.com/library/swift-20-collections-and-control-flow/control-flow-with-conditional-statements/working-with-switch-statements

i am getting the following error : error: expression pattern of type 'String' cannot match values of type 'String?' case "BEL": europeanCapitals.append(world[key])

error: value of optional type 'String?' not unwrapped; did you mean to use '!' or '?'?

switch.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 {
    // Enter your code below
        switch world [key] {
       case "BEL": europeanCapitals.append(world[key])
       case "LIE": europeanCapitals.append(world[key])
       case "BGR": europeanCapitals.append(world[key])
       case "USA": otherCapitals.append(world[key])
       case "MEX": otherCapitals.append(world[key])
       case "BRA": europeanCapitals.append(world[key])
       case "IND": otherCapitals.append(world[key])
       case "VNM": asianCapitals.append(world[key])
    }
    // End code
}

3 Answers

Hi Ahmet,

You want to be swtiching on the key and appending the value associated with that key.

The for loop provides you with each key/value pair one at a time, you then switch on the key. You can also combine multiple cases rather than coding each one, which is repetitive:

    switch key {
      case "LIE", "BEL", "BGR": europeanCapitals.append(value)
      case "IND", "VNM":  asianCapitals.append(value)
      case "BRA", "MEX", "USA": otherCapitals.append(value)
      default: break
    }

You could use the last set of cases in the default, I guess, but you see the idea!

I hope that helps,

Steve.

Something like:

switch key{
      case "BGR", "BEL", "LIE": europeanCapitals.append(value)
      case "IND", "VNM": asianCapitals.append(value)
      default: otherCapitals.append(value)
 }
Zach Swift
Zach Swift
17,984 Points

Just at first glance, I noticed you weren't using the "default" for other countries. I'm not sure off the top of my head if that's required.

Also, not related to the error, I noticed yu are appening to the list like .append(world[key]) When you already have acces to "value" inside the loop so you could do .append(value).

I'll have more time to loo at this a little later.

Ahmet GULER
Ahmet GULER
7,181 Points

Hello,

yes as you have mentioned, when i replaced the statement 'switch world [key]' with 'switch key' the problem was solved.

And thanks for the nice tips

Best Regards,

Ahmet