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 trial

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,407 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]
}