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

I think I am close of this question, but I could be wrong. Not sure how to happen the value to the arrays.

I think I have done the switch statement correctly, but I am not sure if I have appended the values correctly. I could be wrong however.

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 {
    // Enter your code below
    switch (key, value) { 
    case "BEL", "LIE", "BGR": print("europeanCapitals")
    case "VNM", "IND": print("asianCapitals")
    default: print("otherCapitals")
    }

    europeanCapitals.append("Brussels, ""Vaduz, ""Sofia")
    asianCapitals.append("New Delhi ,""Hanoi")
    otherCapitals.append("Washington D.C,""Mexico City, ""Brasilia")


    // End code
}

3 Answers

Sarah Hurtgen
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Sarah Hurtgen
Treehouse Project Reviewer

Hey!

You're really close with your code, but there's a couple spots that should be tweaked for this challenge.

your switch statement right now says

switch (key, value) {

but we really only need to switch on the "key" so that we can append the "value". This way, you don't have to manually write out the values to append them, you can simply refer to them as "value". With this set up, it allows you to append right in the "for in" statement.

For example:

var example: [String] = []
var otherExample: [String] = []

let examples = [
    "1st": "First example",
    "2nd": "Second example",
    "3rd": "Third example"
]

for (key, value) in examples {
    // Where you would enter your code 
    switch key {
        case "1st": example.append(value)
    default: otherExample.append(value)
    }
}

Hope that helps!

Hi, I am still not getting the question correct, I am struggling here.

Sarah Hurtgen
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Sarah Hurtgen
Treehouse Project Reviewer

Do you know which part you are getting stuck on? I would suggest assembling your code in a playground (if you aren't already), and see which errors are coming up. Sometimes the error messages can help show you which spots you need to alter to get things working. Also re watching the videos can often help things click.

In the code you have posted, you need to alter your switch statement to switch on "key" instead of on "(key, value)". Your "append" code needs to go where you have your "print" functions, and instead of manually typing out all the items you want to append, you will append "value".

This takes the "key", which in this case is the abbreviated capitals, and then appends the corresponding "value" to each array.

So at the moment my code looks like this;

case "BEL", "LIE", "BGR": europeanCities.append("Brussels", "Vaduz", "Sofia")

Clearly this is wrong as I am getting an error in the playground but I don't know how to fix this.

I have just realised my mistake thank you.