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

Kathleen Loden
838 PointsAccessing a dictionary for use in the fizzbuzz exercise
Finally found what I needed after much trial, error, corrections from xcode, red herrings, and hair pulling.
But I'd like to understand the reason for the syntax.
I wanted to use a dictionary in my assignment, and while putting values into them is quite straightforward, getting them back out the same way they were before was not.
Apple's documentation had a bunch to say on updating and removing, but I didn't see much on simply accessing/displaying.
For the record, my code is
var fizzbuzzier = [3: [true:"Fizz", false: ""], 5: [true:"Buzz", false: ""]]
for n in 1...21 {
if n % 3 == 0 || n % 5 == 0 {
println("\(fizzbuzzier[3]![Bool(n % 3 == 0)]!)\(fizzbuzzier[5]![Bool(n % 5 == 0)]!)")
}
}
I also came up with
var fizzbuzzier = [3: [true:"Fizz", false: ""], 5: [true:"Buzz", false: ""]]
for n in 1...21 {
if n % 3 == 0 || n % 5 == 0 {
for i in fizzbuzzier {
print("\(i.1[Bool(n % i.0 == 0)]!)")
}
println()
}
} // EDIT: Just realized that this results in "BuzzFizz" when both criteria are met. Have I made a mistake, or is it iterating through in reverse order?
//More trial and error, I could fix it with:
//
//for i in [3,5] {
// print("\(fizzbuzzier[i]![Bool(n % i.0 == 0)]!)")
// }
but this was a bigger departure from what I'd already learned (it was the result of one of the trials/errors)
So why is it, I can create a dictionary with
var thing = [1:"one"] //[1: "one"]
add to it with
thing[2] = "two" //{Some "two"} <-- same phenomenon, but results in thing == [1: "one", 2: "two"]
display it normally with
thing //[1: "one", 2: "two"]
but if I type
thing[1] //{Some "one"}
I get {Some "one"}, which is not simply a different way of displaying it in the editor, but is in fact some other kind of object?
In order to access it, I need to type
thing[1]! //"one"
with an ! after the [index]
Why does it change to something else once you access it directly?
1 Answer

luckyluckyluke
2,683 PointsYou have to add an exclamation mark because accessing a dictionary returns an optional value. That way you can check if an item actually exists. In your examples you just assume/know that the items exist, but in reality you often want to make sure that the item exists before you use it.
Check this code. It creates your dictionary and then safely prints the value of thing[1]. Play around with it, rename the key '1' to see how it acts in case of a missing key:
var thing = [1:"one"]
thing[2] = "two"
if let possibleThing = thing[1] {
println(possibleThing)
} else {
println("The item does not exist")
}