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 Enumerations and Optionals in Swift Introduction to Optionals Nil Values in Collections

Tyler Dotson
PLUS
Tyler Dotson
Courses Plus Student 1,740 Points

I don't know how to unravel the dictionary.

How do I unravel the dictionary?

optionals.swift
let movieDictionary = ["Spectre": ["cast": ["Daniel Craig", "Christoph Waltz", "LÊa Seydoux", "Ralph Fiennes", "Monica Bellucci", "Naomie Harris"]]]

var leadActor: String = ""

// Enter code below

if let movieDictionary = movieDictionary["spectra"], if let movieDictionary = movieDictionary["cast"], let leadActor = movieDictionary[0]

It looks like your using nested if statements and the alternate syntax of optional unwrapping at the same time. It makes your code look a bit confusing and could be a cause of your bug. I recommend choosing one way or the other. Since cast is the last nested dictionary in the bunch, we do not have to unwrap any further. Once let movieDictionionary = ["cast"] is successfully unwrapped and stored, you can enter your code block and then update the variable for leadActor.

So something like if let spectra = ...., let spectra[cast]=... { then update the leadActor}

1 Answer

Jhoan Arango
Jhoan Arango
14,575 Points

Hello:

Well you almost got it. All you had to do was close the if let statement with the braces and then assign the value of the array to leadActor.

if let movie = movieDictionary["Spectre"], let cast = movie["cast"] {
    leadActor = cast[0]
}

Good luck