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

Working With Switch Statements

Hi guys, Can someone help me? I think I wrote my code 100% correctly, but the code challenge says that I did not append the results to the correct array. Thanks.

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 {
    case "BEL","LTE","BGR": europeanCapitals.append("Brussels","Vaduz","Sofia")
    case "IND","VNM": asianCapitals.append("New Delhi","Hanoi")
    default: otherCapitals.append("Washnigton D.C.","Mexico City","Brasilia")
    }
    // End code
}

3 Answers

xiaoluke
xiaoluke
6,387 Points

Right i'm trying to figure this out, It's been awhile since I used swift but digging through the Swift 3.0.1 Documentation regarding appending I came across this.

alternatively, append an array of one or more compatible items with the addition assignment operator (+=):

shoppingList += ["Baking Powder"]

// shoppingList now contains 4 items

shoppingList += ["Chocolate Spread", "Cheese", "Butter"]

// shoppingList now contains 7 items

https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html

You could try that, what is the exact question that the task is asking you to do?

Thanks Luken!

Lana Wong
Lana Wong
3,968 Points
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 "IND","VNM":
        asianCapitals.append(value)

    default:
        otherCapitals.append(value)
    }

    // End code
}

I got stuck with this too. You need to append the values of the keys. There is like a relationship between key and value, as seen in the for-loop. The code is iterates over and over with the keys identified as key, and their values identified as value. For example, if the loop iterates over the code for the first time, it finds the "BEL" key, and appends the value that goes with the key to the europeanCaptitols array. The value would be "Brussels". If the loop iterates over the code a second time, it finds the "LIE" key, and appends the value that goes with that key, "Vaduz".

Hope this helps.

Thanks Lana! Your comment helped me out a lot!

xiaoluke
xiaoluke
6,387 Points

I think I found your answer,

In your enum(with all the codes and locations it is "LIE": "Vaduz" aka l i e

where as when you append you have written L T E. Took my awhile to find it i think it's just down to simply typo.

Thanks so much Luken! I didn't see that typo there.

Sadly it still says for me to "Remeber to append the values from each case statement to the correct array"