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 trialJon Schenck
2,028 PointsHelp with dictionary values
Can't get this code to work
struct ChocolateBox {
var caramelDelight: [String]
caramelDelight = [flavor : caramel]
}
2 Answers
adrianfraisse
2,467 PointsHi,
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
2,028 PointsThanks for the help
adrianfraisse
2,467 PointsYou're welcome ! Please tag the answer as accepted if you're satisfied by it !