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 Enumerations and Optionals in Swift Introduction to Optionals Nil Values in Collections

Arius Eich
Arius Eich
2,769 Points

Array Syntax Script

I have been stuck on this question for a while and the error it keeps giving me is:

"Make sure that you're using array subscript syntax to get the value for the lead actor"

I tried to use both methods they showed in the previous video but I couldn't get either to work the sufficient for the challenge. Thank you!

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 roles = movie["cast"], let leadRole = movieDictionary["Daniel Craig"] {
    let leadActor = movieDictionary["Daniel Craig"] 
}

/*
if let movie = movieDictionary["Spectre"] {
  if let roles = movie["cast"] {
    let leadActor = movieDictionary["Daniel Craig"]
  }
*/

2 Answers

Matthew Long
Matthew Long
28,383 Points

You're not far from the solution. The first issue is you're trying to unwrap too far. Secondly, the lead actor, as specified by the challenge description, is at index position 0. Therefore, instead of trying to use the string "Daniel Craig" as your subscript use 0.

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