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

Edward Sapp
Edward Sapp
8,479 Points

What am I doing wrong in my nested if let statement?

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 leadActor = cast[0] }

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

2 Answers

Dan Lindsay
Dan Lindsay
39,611 Points

Hello Edward,

We definitely need to do an if let nested in another if let for this one. First, we need to see if the movie exists, so we do this:

if let movie = movieDictionary[Spectre] {

}

If that exists, our movie constant now holds the data for “Spectre”. Now we need to see if the cast exists, so we do this:

if let movie = movieDictionary[Spectre] {
    if let cast = movie[cast] {

    }
}

and if it’s there, it will be assigned to our constant cast. Now we just need to grab the first index of cast, and assign it to our leadActor, like so:

if let movie = movieDictionary[Spectre] {
    if let cast = movie[cast] {
        leadActor = cast[0]
    }
}

This should pass the challenge. I do find working with dictionaries(and Optionals, for that matter) can be quite challenging, especially when it’s a dictionary within a dictionary within a dictionary(and so on). Once you start to work with parsing JSON data(if you are not already) from the web(working with different API’s), you’ll find that a lot of the data seems to be stored in dictionaries, so this is important stuff to learn.

Hope this helps you out!

Dan

Edward Sapp
Edward Sapp
8,479 Points

Thanks, Dan! I have not yet begun work with JSON. In fact, it doesn't seem like Treehouse has a course for working with JSON & APIs in Swift. Would you recommend any good programs to branch off in that direction?

Awesome break down, this actually really helped me understand the concept and helped with the confused look on my face reading the challange xD.

Dan Lindsay
Dan Lindsay
39,611 Points

You are welcome Edward!

Treehouse has this one:

https://teamtreehouse.com/library/network-programming-with-swift-2

It is Swift 2, so some of the code will be different with Swift 3, but it still will give you a chance to get into parsing some data! Even doing the JSON specific courses on here is a good idea, even if they are not Swift related.

All the best!