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

boris said
boris said
3,607 Points

I am getting a lot of errors in X-CODE. e.g Extra argument in call.

I am getting errors on my case statements and all of my default statements. Here is my code:

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


    default: print("Do nothing")
    }
    }

[MOD: edited code block. sh]

boris said
boris said
3,607 Points

Sorry about my code being compacted. Here is the new version (ignore code from challenge)

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 world {
      case "BEL", "LIE", "BGR": europeanCapitals.appendContentsOf("Brussels", "Vaduz", "Sofia")
      case "VNM", "IND": asianCapitals.appendContentsOf("New Delhi", "Hanoi")
      default: otherCapitals.append(world)
    }   
}

11 Answers

Yep. So, again, you want to switch on key; use switch key (I've commented your code).

The compiler is saying you can't match a String, the individual country codes like "BEL" or "IND" , to a String : String which is what world is. You need to match a String to a String, like key.

Steve.

No problem! :+1:

Hi Boris,

You're correctly passing the key and value into the for loop.

Try switching on the key and appending the value associated with that key.

So you're going through each key/value pair. At each iteration you are parsing through one key and value. So, switch key isolates which route your code should take via the cases you have created. So, append to the appropriate array using case "BEL", "LIE", "BGR": europeanCapitals.append(value).

I hope that helps out - I didn't want to just give you the answer as you've done the hard bit already!

Steve.

Hi Boris,

Can you post your amended code, please?

I could put the solution up, but think there's more learning to be done by going through your code first.

First,each iteration of the for loop deals with one key/value pair. Your switch statement is able to deal with them all. But only one goes through each time the code loops.

You are switching on the key - that's the "BEL", "IND" bit. You then append the value of that key into the array: case "BEL", "LIE", "BGR": europeanCapitals.append(value)

Post your code and let's go through it to reach the solution.

Steve.

OK. So, you want to switch on key, not world. Each key is then tested by your case statements. And amend all your append method to use value as a parameter. Your last one doesn't.

And I think that's it!

boris said
boris said
3,607 Points

Hmmmm, I am still getting the same error. I tried switching the key and value but still the same error. Also, I tried it with just one key and value. I think I understand the error more. I am pretty sure the compiler won't let the array of the capitals be of type String: String which is the type of the array, in order to make it work, I believe I would have to extract the value separately (if at all possible) or change the type to String

boris said
boris said
3,607 Points
for (key, value) in world {

    switch world {

    case "BEL", "LIE", "BGR": europeanCapitals.append(value)

    case "VNM", "IND": asianCapitals.append(value)

    default: otherCapitals.append(world)

    }
boris said
boris said
3,607 Points

Hmmm, I completed that and am still getting errors, could my x-code be out of date?

What are the errors?

boris said
boris said
3,607 Points

Expression pattern of type 'String' cannot match values of type '[String: String]'

This error occurs for each key I try to pass through.

I'll try it in Xcode - did you pass the challenge, though?

Steve.

Hang on ... that error is saying that you're trying to match a String to a String : String.

i.e. you're switching on world still, which is a String : String, rather than key, which is a String?

Can you paste your code again, please?

The code works fine in Xcode - I think you've still got switch world?

boris said
boris said
3,607 Points

No, it says my code can't be compiled and when I look in preview the error is the same.

Yes, I think because you aren't switching on key but are still using world, a String : String.

You want to use switch key then the rest is mainly OK.

boris said
boris said
3,607 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 {
    switch world { // <-- change world to key
    case "BEL", "LIE", "BGR": europeanCapitals.append(value)
    case "VNM", "IND": asianCapitals.append(value)
    default: otherCapitals.append(value)
    }

}