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 Swift 2.0 Enumerations and Optionals Introduction to Optionals Optional Binding

Stephan Porsche
Stephan Porsche
2,864 Points

I'm getting the error "Initializer for conditional binding must have Optional type, not 'String" in the following task.

See my code here:

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

4 Answers

Marina Alenskaja
Marina Alenskaja
9,320 Points

Hi Stephan

Seems like you are trying to get a value out of the key "cast" twice - here is the correct approach:

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 moviecast = movie["cast"] {
        leadActor = moviecast[0]
}
Stephan Porsche
Stephan Porsche
2,864 Points

You're dead right! What was I thinking... Thanks alot. :)

Hi Alexandru Stefanescu , Actually you have put (curly braces) i.e. "{" , whereas it should be a (coma) i.e. "," . and also the questions asks you to store the result in a variable "leadActor" which is already declared, if you see to the top - ( var leadActor )

So code is ``` html if let movie = movieDictonary[" Spectre "] , if let movieCast = movie[" cast "] { leadActor = movieCast[0] }

Alexandru Stefanescu
Alexandru Stefanescu
2,195 Points

Thanks, I should have paid more attention :)

I'm a bit confused. I get that we need to check if the key returns a value, but couldn't the same logic apply to the array inside cast? What if it was an empty array and position 0 was nil?

Jake Adams
Jake Adams
1,608 Points

You are correct. If the array was empty, you'd get an "array index out of range" exception. However, I don't think a 'let' optional binding is the answer. You'd have to explicitly check the array count inside the if clause

let movieDictionary = ["Spectre": ["cast": Array<String>()]]

var leadActor: String?

if let movie = movieDictionary["Spectre"], let cast = movie["cast"] {
  leadActor = cast.count > 0 ? cast[0] : nil
}
Alexandru Stefanescu
Alexandru Stefanescu
2,195 Points

Hello all :),

any idea why this is not accepted?

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"] {
   if let movieCast = movie["cast"] {
        let actor = movieCast[0]
    }

}
James Killeen
James Killeen
Courses Plus Student 2,207 Points

Hi Alexandru, I believe your problem here is the addition of {} instead of comma, after the ["Spectre"]. Then on your second line, you have used an "if let" instead of just a "let"