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

Maxence Roy
Maxence Roy
8,753 Points

Value does not get assigned

Hi,

I'm trying to assign the string "Daniel Craig" to the leadActor value. It's like I can assign it inside the function, but then I try to print the constant and it's not propagating outside the function.

What should I do ?

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

var leadActor: String = ""

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

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! You're really close here, but you're redefining the leadActor variable. It was declared outside the if let statement but inside the code block there, you're defining it again, which means it will be local to that statement.

This is what you're looking for:

//if these assignments were successful set the leadActor variable to this
leadActor = casting[0]

Hope this helps! :sparkles: