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

Not sure what I am doing wrong with the optional binding quiz.

Please review my code.

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 movie = movieDictionary["Spectre"],
 let leadActor = movie["cast"[0]] {}

3 Answers

Steven Deutsch
Steven Deutsch
21,046 Points

Hey Erik Huang,

This was a tough one for me to follow as well. Let me see if I can help. The problem I can see is in your second let statement. You can't pass both a key of "cast" and an index of 0 in for this statement. You have to break them up.

Using the movie constant you created with the first if let statement, you want to access the value associated with the "cast" key, which is an array of Strings. You assign the value you receive from this key, the array, to another constant - I will use the constant actors in my example below - and THEN you can access the first item in the array using the index.

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 movie = movieDictionary["Spectre"],
    let actors = movie["cast"] {
        leadActor += actors[0]
    }

I think my explanation was a bit rough so let me know if I need to clarify this for you. It's easier to see what's going on just by looking at the code.

Good Luck!

Luke Davis
Luke Davis
1,488 Points

Thanks a ton! This one really stumped me too.

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,631 Points

Thanks Steven Deutsch!

For some reason, that one threw me... I was trying to dig one level deeper than I need. Your explanation cleared it right up for me. (Marked now as "Best Answer")

:)

Steven Deutsch
Steven Deutsch
21,046 Points

I'm glad my response was clear enough to help you. Thanks for the Best Answer marking.

hey Steven Deutsch why did you do

leadActor += actors[0]

instead of just

leadActor = actors[0]

?

Steven Deutsch
Steven Deutsch
21,046 Points

Hey Andrew,

Both work fine. Just how I decided to write it in my response at the time. I might've thought that leadActors was an array when I was answering the question, which is why I decided to add it that way.