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

Aramik YOUSEFZADEH
PLUS
Aramik YOUSEFZADEH
Courses Plus Student 895 Points

Getting code error

It says I'm not assign to the correct array.

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" : europeanCapitals.append(world[key])
    case "LIE" : europeanCapitals.append(world[key])
    case "BGR" : europeanCapitals.append(world[key])
    case "IND" : asianCapitals.append(world[key])
    case "VNM" : asianCapitals.append(world[key])
    default: otherCapitals.append(world[key])




    }
    // End code
}
Jason Anders
Jason Anders
Treehouse Moderator 145,860 Points

Moderator deleted duplicated post that was posted 5 minutes prior to this one. Please try not to post multiple threads with the same question.

2 Answers

Thomas Dobson
Thomas Dobson
7,511 Points

Hi Aramik,

Your were close and had the right idea... Note you want to append the VALUE for the key defined in your case statement. In your case you tried to append the key... and subsequently used slightly incorrect syntax on top of that.

See below revisions:

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" : europeanCapitals.append(value)
    case "LIE" : europeanCapitals.append(value)
    case "BGR" : europeanCapitals.append(value)
    case "IND" : asianCapitals.append(value)
    case "VNM" : asianCapitals.append(value)
    default: otherCapitals.append(value)
    }
}

In a playground we can verify our results

europeanCapitals // returns ["Vaduz", "Brussels", "Sofia"]
asianCapitals // returns ["Hanoi", "New Delhi"]
otherCapitals // returns ["Mexico City", "Brasilia", "Washington D.C."]