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

Jack Ankeny
Jack Ankeny
2,043 Points

Help with Nil Values in Collections

When running my code in Xcode it is returning the correct result of "Daniel Craig", but I cannot get my code to pass the challenge.

Any help would be appreciated.

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"], if let castList = movie["cast"] {
    let leadActor = castList[0]
}
Jack Ankeny
Jack Ankeny
2,043 Points

Sorry, the second "if" is not supposed to be there, that got entered when messing with the code.

Should read like this:

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

6 Answers

Dave Berning
Dave Berning
17,365 Points
let movieDictionary = ["Spectre": ["cast": ["Daniel Craig", "Christoph Waltz", "Léa Seydoux", "Ralph Fiennes", "Monica Bellucci", "Naomie Harris"]]]

var leadActor: String = ""

if let movie = movieDictionary["Spectre"] {
   if let cast = movie["cast"] {
      leadActor = cast[0]
   }
}
Jack Ankeny
Jack Ankeny
2,043 Points

Thanks! I missed the fact that each "if let" needed a separate {}.

Much appreciated

I got this working on Swift 3 after removing constants definition "let" from the line of code "let leadActor = cast[0]"

I've only been coding for two months so I'm also still confused with everything. :) Maybe it works like this because the variable "leadActor" we've already created earlier and so now it only needs to be assigned with new value from cast[0]

Devon F
Devon F
9,615 Points
let movieDictionary = ["Spectre": ["cast": ["Daniel Craig", "Christoph Waltz", "Léa Seydoux", "Ralph Fiennes", "Monica Bellucci", "Naomie Harris"]]]

var leadActor: String = ""

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

This should be another concise way to write it, as you can combine if let statements by putting a comma after the first one, and simply writing let before the curly braces. :)

Xavier Avery
Xavier Avery
8,059 Points

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

var leadActor: String = ""

if let movie = movieDictionary["Spectre"], let castList = movieDictionary["cast"] { let leadActor = movieDictionary[0] } hey I'm lost in Swift 3 version

Vanessa Rodriguez
Vanessa Rodriguez
6,685 Points

Thanks Matias Pehkonen !!! That's what the issue was for me too, removing the "let" !

Jenny Dogan
Jenny Dogan
4,595 Points

Anyone know why we should write leadActor = castList[0] and not let leadActor = castList[0]?