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

abdirahman liban
PLUS
abdirahman liban
Courses Plus Student 2,163 Points

i ned help

i ned help

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 key {
    case ["BEL"],["LIE"],["BGR"]: europeanCapitals.append("Brussels" , "Vaduz" , "Sofia" )
    case ["USA"],["MEX"] , ["BRA"] : otherCapitals.append ("Washington D.C." , "Mexico City" , "Brasilia")
    case ["IND"], ["VNM"] : asianCapitals.append ("New Delhi" ,"Hanoi" )
    }
    // End code
}

2 Answers

Keli'i Martin
Keli'i Martin
8,227 Points

Currently, you are trying to append multiple values to your array when your switch statement finds the passed in key. What you really want to do is only append the value that is being passed into the for loop. For example:

for (key, value) in world {
    switch key {
        case "BEL", "LIE", "BGR": europeanCapitals.append(value)

Also, you don't need the brackets around the keys. Putting brackets around them is making them an array containing one string, which is not what you are passing into the for loop.

Hope that helps!

good evening abdirahman liban :) your code must be like this :

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 key { case "BEL","LIE","BGR": europeanCapitals.append(value) case "USA","MEX" , "BRA" : otherCapitals.append (value) case "IND", "VNM" : asianCapitals.append (value ) default: () //put some default } // End code }

in Switch you must add the default value or put just empty Brackets ()

also i want you to explain what do you want to do with your code ? what does your code do ? because i think that there is a mistake with using append function in case that you applied . . .

إذا تتكلم عربي يكون أحسن عشان أشرح لك :) وأتمنى إني أفدتك :)