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

Michael Bau
Michael Bau
3,394 Points

How do I interpret 'Key' and 'Value'?

Hi,

I have no idea as to how to interpret the (Key, Value) in the 'for in' statement!

Are we supposed to leave it as is i.e '(Key, Value)' or change it to something else e.g. 'country' or does Swift 'know' that 'Key' is the key in the 'world' constant and 'Value' is the value?

We haven't really had any examples of picking up values from variables or constants containing multiple values pertaining to the same instance such as "BEL": "Brussels" (I am not sure if that makes any sense to the reader, but it's as precise as I can explain it).

It seem that no matter what I do, I get the message: ‘Remember to append the the values from each case to the correct array’, which isn’t really all that helpful!

Best regards, Michael

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 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
}

2 Answers

Hi Michael,

You'll come across key/value pairs quite a bit when dealing with collections of data. Essentially, the key is a short mnemonic that 'unlocks' the value, which can be a huge chunk of data, or just a string, as in this example.

To use the key, in this example, you would pass it into the collection, world and that will return the value. Like this:

value = world["BEL"]

In the for loop, you can use this approach or you can pull out both key and value at the beginning of the loop. I'm pretty sure that the words key and value are just variable names, so you could use any names, but there's no added readability by doing so. This gives us two approaches to populate the correct data:

for key in world {
  switch key{
    case "BEL": europeanCapitals.append(world[key])
  }
}

// or 

for (key, value) in world {
  switch key{
    case "BEL": europeanCapitals.append(value)
  }
}

Does that answer your question? If not, shout back and I'll try again!!

Steve.

First example isn't working ... bear with me!

I'll not edit the answer; there's no point.

You can use the world[key] method to access the dictionary, but it is an optional, so you need the !. And it seems the loop always wants the key and value pulling out of it to do this, which makes this access method somewhat pointless!

for (key, value) in world {
    // Enter your code below
    switch key {
      case "BEL", "LIE", " BGR": europeanCapitals.append(world[key]!)
      case "IND", "VNM": asianCapitals.append(world[key]!)
      default: otherCapitals.append(world[key]!)
    }
    // End code
}

I could probably try something different in a playground but I'll not labour the point.

The two variable names are just that, names. This works fine:

for (code, name) in world {
    // Enter your code below
    switch code {
      case "BEL", "LIE", " BGR": europeanCapitals.append(name)
      case "IND", "VNM": asianCapitals.append(name)
      default: otherCapitals.append(name)
    }
    // End code
}

My Swift is rusty - not used it in far too long!

Michael Bau
Michael Bau
3,394 Points

Hi Steve,

Thanks again for helping me out!

I tried your first suggestion in the code challenge and it worked when I added the default statement.

I will try the other suggestions in the playground, but already now your feedback has given me a much better understanding of the use of key/value pairs!

Best regards, Michael

Good stuff! My code examples are usually just that, examples to illustrate a point. They often contain a few syntactical issues but I use it to try to get my point across - I don't profess to be a coding God!

Unordered collections are common. You'll come across them a lot. They're easier to use than ordered collections, like arrays, and very easy to convert into XML or JSON to use in APIs or web apps. They come with a number of useful shorthand methods that make accessing, adding and removing very easy. Arrays can be fiddly and easy to get wrong as they're ordered, which is a constraint at times.

I'm not sure about iOS dictionaries, i.e. the iOS library for Apple devices, rather than the Swift language itself. But in Android, dictionaries are used for simple things like passing data between Activities (Views, in iOS speak) via extras. I don't remember enough iOS to recall if that's the same but it wouldn't surprise me.

Anyway, as I said, you'll come across dictionaries a lot so the concept of a key/value pair will become more familiar as you progress through the courses.

:closed_book: = :+1: