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

Ian Henderson
Ian Henderson
5,512 Points

Tried challenge w/ 2 different coding approaches that both work in XCode. Both cases get "could not be compiled" error.

The error says to check "preview" for compiler errors, but in both cases there is no text in the "preview" view. One approach is attached, the other is as follows:

let movieDictionary = ["Spectre": ["cast": ["Daniel Craig", "Christoph Waltz", "Léa Seydoux", "Ralph Fiennes", "Monica Bellucci", "Naomie Harris"]]]

if let movieName = movieDictionary["Spectre"], let movieTeam = movieName["cast"] { var leadActor: String = movieTeam[0] }

optionals.swift
let movieDictionary = ["Spectre": ["cast": ["Daniel Craig", "Christoph Waltz", "Léa Seydoux", "Ralph Fiennes", "Monica Bellucci", "Naomie Harris"]]]

if let movieName = movieDictionary["Spectre"] , let leadRole = movieName["cast"]{
    var leadActor: String = leadRole[0]
}

1 Answer

Jorge Solana
Jorge Solana
6,064 Points

Hey Ian

Variable leadActor is already written on top of the code challenge, so you don't need to define it again in the if let section. Xcode works because it should declare a local variable to the if statement.

Other than that, I don't see any other issue.

Keep up the good work!