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 2.0 Collections and Control Flow Control Flow With Conditional Statements Working with Switch Statements

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 '?'?

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,


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,972 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.

7,181 Points


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,