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

Optionals, 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?

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 actor = movie["cast"] {
    var leadActor = actor[0]
}

2 Answers

Steven Deutsch
Steven Deutsch
21,046 Points

Hey 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!

My duh. Thanks, Steven.