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

Roop P
PLUS
Roop P
Courses Plus Student 6,937 Points

Again this doesn't make sense. The code works.

This works, what is wrong. TTH needs to improve this process. The questions are not clear on results and to verbose.

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 anActor = movieDictionary["Spectre"]?["cast"]?[0] {
    leadActor = anActor
    print(leadActor)
} else {
    print("Error: Contains nil")
}

2 Answers

Matthew Long
Matthew Long
28,407 Points

The challenge is checking that you use an if let chain to unwrap each consecutive operation. Also, the challenge never asked you to print anything.

if let movie = movieDictionary["Spectre"], let cast = movie["cast"] {
  leadActor = cast[0]
}
Roop P
PLUS
Roop P
Courses Plus Student 6,937 Points

Matthew,

Thanks again - You are the man!

I see I over did it. Again for me, these questions are misleading - I took the "unwrap each consecutive operation" too seriously, but instead I should have used an if-let sequence. I was wondering why the question asked for something which was not covered in the course.

P.S. I use prints to test and forgot to remove it in my frustration