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

Jos Feseha
Jos Feseha
4,011 Points

switch statement ,I am stucked here please help

how do I do this switch statement ....

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

    // End code
}

2 Answers

Rogier Nitschelm
seal-mask
.a{fill-rule:evenodd;}techdegree
Rogier Nitschelm
iOS Development Techdegree Student 5,461 Points

I made a little error myself, apologies for that.

The otherCapitals would be ideal for a default case, because then we would only have to make the following:

case "BEL": // add all european countries here
  europeanCapitals.append(value)
case "IND": // add all asian countries here
  asianCapitals.append(value)
default: // the rest automatically goes here
  otherCapitals.append(value)
Jos Feseha
Jos Feseha
4,011 Points

ohhw ok , Thank You , it worked perfectly !

Rogier Nitschelm
seal-mask
.a{fill-rule:evenodd;}techdegree
Rogier Nitschelm
iOS Development Techdegree Student 5,461 Points

Perhaps something 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 {
    switch key {
        case "BEL":
            europeanCapitals.append(value)
        case "IND":
            asianCapitals.append(value)
        default:
            break
    }
}

Because I do not entirely want to ruin the joy of figuring some additional things out: It is possible to put multiple cases on a single line, so you only need a total of 2 cases plus a default case.

Also, the default case is ideal for appending a capital to an array with capitals too. Which array of capitals would be fit for use as the default case?

Jos Feseha
Jos Feseha
4,011 Points

THANK YOU So much ! , I think the asianCapitals will fit for the default case .