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

Stuart Walker
Stuart Walker
4,649 Points

Need assistance with swift statement challenge. It's driving me crazy!

I can't see what else I need to add to this code in order for it to pass the challenge. Any help would be much appreciated

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 (capitalCity) in world {
    // Enter your code below
    switch capitalCity {
      case "BEL", "LIE", "BGR": europeanCapitals.append(not the key!)
      case "IND", "VNM": asianCapitals.append()
      default: otherCaptials.append()
    }
}

2 Answers

Nathan Tallack
Nathan Tallack
22,160 Points

You were quite close. Consider the changes to your code below.

for cityCode, cityName in world {  // This is a dictionary so uses key, value pairs.
    switch cityCode {
      case "BEL", "LIE", "BGR": europeanCapitals.append(cityName)
      case "IND", "VNM": asianCapitals.append(cityName)
      default: otherCaptials.append(cityName)
    }
}
Stuart Walker
Stuart Walker
4,649 Points

Still can't seem to pass the challenge. Not quite sure what to do next.

Nathan Tallack
Nathan Tallack
22,160 Points

Righto. So let's simplify what you need to input, and understand it. :)

You already have all the code there in a fresh challenge screen. Be sure to reload it in your browser so that it is fresh.

Just like this:

// Enter your code below

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

// Enter your code above

Stuart Walker
Stuart Walker
4,649 Points

Ok I got it to work. Thanks for your help Nathan!

Stuart Walker
Stuart Walker
4,649 Points

Ok I got it to work. Thanks for your help Nathan!