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 Dictionaries Working with Dictionaries

Syntax Help

Is this the right syntax for a dictionary, It is not working in the playground so I have no idea what I'm doing wrong.

dictionary.swift
// Enter your code below
 let iceCream = [
"CC": "Chocolate Chip"
"AP": "Apple Pie"
"PB": "Peanut Butter"
]

2 Answers

Jhoan Arango
Jhoan Arango
14,575 Points

Hello : Tejas Pulavarti & Cindy Lea

Cindy is right, you have to use var instead of let, because that's what the challenge wanted you to do. But that's not what is wrong with your syntax. You can use a var or a let to declare a dictionary, depending on your needs.

When declaring a dictionary you want to use the [ ] brackets. Inside the brackets, you have to give them keys and values, separating the key and the value with colons : once you've done that, you have to separate them with commas, that's where your code was wrong.

Example.

// Proper declaration of a dictionary.

let airports: String = [
"LGA": "La Guardia", // Comma separation
"MIA":"Miami Airport", 
"LAX":"Los Angeles Airport"
]


// Accessing the values

let miamiInternational = airports["MIA"]

Good luck.

Cindy Lea
PLUS
Cindy Lea
Courses Plus Student 6,497 Points

// Enter your code below

var iceCream = ["CC": "Chocolate Chip", "AP": "Apple Pie", "PB": "Peanut Butter"]

You need to use var, not let