Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

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,485 Points

// Enter your code below

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

You need to use var, not let