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 Basics (retired) Collections What is a Dictionary?

can someone explain this in more depth please?

stuck on challenge task 2 of 2

Caleb Kleveter
Caleb Kleveter
Treehouse Moderator 37,862 Points

What is your code and error? There are some great posts on this challenge in the forum. Just search the challenge name in the forum search bar.

1 Answer

Jhoan Arango
Jhoan Arango
14,575 Points

Hello Harley: This was an answer given to another student. It may help you understand how to access values from a dictionary using their keys.

In a dictionary you have a key and a value, in order for you to access the value of a dictionary you have to use it's key.

**let dictionary = ["Key":"Value"]

So for example :

// Let's create a dictionary of cars
let cars = ["Key 1":"Ford Fiesta","Key 2":"BMW X5", "Key 3":"Audi S4"]

// Now let's access a car by using it's corresponding key.
cars["Key 1"]

// With this key, I am getting "Ford Fiesta".

/*
   Ok so now let's say that you want to go riding a car.
   This is capturing the value of "Key 2" and assigning it to a constant named bmw.
*/

let bmw = cars["Key 2"]

// Now you can use this constant in other places, for example. 
println("Harley likes to ride his new \(bmw)")

This will print Harley likes to ride his new BMW X5.

Hope this explanation helps you understand a bit better and solve your challenge.

Good luck