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 trialNoe Arzate
Python Web Development Techdegree Student 6,077 Pointswhy won't my code pass the code challenge using if let? it works fine in xcode.
My code for if let code challenge works fine in xcode but not in the code challenge workspace. What am I missing?
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"] {
print(leadActor[0])
}
3 Answers
Matthew Connolly
iOS Development Techdegree Student 11,595 PointsHello Noe,
You are so close! Instead of printing the lead actor the challenge is asking you to assign the lead actor to the variable leadActor
.
if let movie = movieDictionary["Spectre"], let cast = movie["cast"] {
/* assign lead actor to variable */
}
Noe Arzate
Python Web Development Techdegree Student 6,077 Pointsif let movie = movieDictionary["Spectre"], let cast = movie["cast"] { /* assign lead actor to variable */
var leadActor = cast[0] }
Hi Matthew, Thank you for your help.
Code snippet is fine in Xcode but won't pass the code challenge. What I am missing? Thanks
Matthew Connolly
iOS Development Techdegree Student 11,595 PointsHi Noe,
The variable leadActor
is already created and set to an empty string, no need for you to create one inside the if let
statement. Instead, just assign cast[0]
to the variable.
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 cast = movie["cast"] {
leadActor = cast[0]
}
Noe Arzate
Python Web Development Techdegree Student 6,077 PointsHi Matthew,
You're absolutely correct. I removed var from leadActor and it passed.
Thank you, Noe