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 Build a Playlist Browser with Swift Building the Music Library and Playlist Models Creating a Data Model

Jon Schenck
Jon Schenck
2,028 Points

Help with dictionary values

Can't get this code to work

ChocolateBox.swift
struct ChocolateBox {
   var caramelDelight: [String]
   caramelDelight = [flavor : caramel]
}

2 Answers

Hi,

Your issue is that the caramelDelight var you created is of type Array instead of being a dictionary.

A dictionary is made up of key/value pairs. Here is the correct initializer syntax to create a dictionary with String keys and String values : var caramelDelight : [String:String]

To add a key/value pair to your dictionary, you can then use the subscript syntax : caramelDelight["flavor"] = "caramel"

Note that the key and value are both Strings.

A quickier way to do this is to initialize the dictionary with literals : var caramelDelight = ["flavor":"caramel"]

In this case, you don't need to specify the dictionary type, Swift will infer it for you.

Hope that helps.

Jon Schenck
Jon Schenck
2,028 Points

Thanks for the help

You're welcome ! Please tag the answer as accepted if you're satisfied by it !