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 trialJack Ankeny
2,043 PointsHelp 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.
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]
}
6 Answers
Dave Berning
17,365 Pointslet 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
2,043 PointsThanks! I missed the fact that each "if let" needed a separate {}.
Much appreciated
Matias Pehkonen
4,158 PointsI 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
9,615 Pointslet 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
8,059 Pointslet 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
6,685 PointsThanks Matias Pehkonen !!! That's what the issue was for me too, removing the "let" !
Jenny Dogan
4,595 PointsAnyone know why we should write leadActor = castList[0] and not let leadActor = castList[0]?
Jack Ankeny
2,043 PointsJack Ankeny
2,043 PointsSorry, 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] }