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 trialRoger Rohweder
8,474 PointsOptionals, nested dictionary binding: code succeeds in Playground, but fails the quiz
My code (below "enter new code") works in a playground, but the quiz says "Bummer! Make sure you are retrieving the string at index 0 in the nested array” My code DOES return “Daniel Craig”. What doesn’t it like?
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 actor = movie["cast"] {
var leadActor = actor[0]
}
2 Answers
Steven Deutsch
21,046 PointsHey Roger Rohweder,
The problem is, you are creating a variable leadActor instead of updating the value of leadActor that is currently an empty string. Remove var and this will fix your problem.
let movieDictionary = ["Spectre": ["cast": ["Daniel Craig", "Christoph Waltz", "Léa Seydoux", "Ralph Fiennes", "Monica Bellucci", "Naomie Harris"]]]
// You need to update leadActor here
var leadActor: String = ""
// Enter code below
if let movie = movieDictionary["Spectre"],
let actor = movie["cast"] {
// Remove "var" here
var leadActor = actor[0]
}
Good Luck!
Roger Rohweder
8,474 PointsMy duh. Thanks, Steven.