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 Nil Values in Collections

David Alberto Fuentes Ortiz
David Alberto Fuentes Ortiz
1,593 Points

I have No Idea what to do

can someone explain me what do i have to do... also can someone help me find a website where i can get projects to practice what i learned already...... THANK YOU.

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 leadActor1 = movieDictionary["cast"], let actorName = leadActor1["cast.0"] {

    leadActor = "\(actorName)"
}
David Alberto Fuentes Ortiz
David Alberto Fuentes Ortiz
1,593 Points

In the editor, you have a pretty complex dictionary representing a movie. It contains a nested dictionary, which itself contains an array of Strings as a value.

Your job is to assign the string containing the value for the lead actor (which for the sake of the example is at position 0 in the array) to the variable leadActor.

Since dictionaries return optional values, you will have to use an if let statement to unwrap each consecutive operation.

3 Answers

Greg Kaleka
Greg Kaleka
39,021 Points

Hi David,

You're actually pretty close. You've got the right idea. Let's think through this piece by piece.

Each layer into this rather complex dictionary, we're going to have to use optional binding (if let). So first step is to get the cast dictionary, which belongs to the key "Spectre". Remember that we use a key to access the value it's associated within a Dictionary.

if let spectreDictionary = movieDictionary["Spectre"] // ...

Make sense? So now we have the dictionary with just one key, "cast", whose value is an array of cast names. We have to use optional binding to unwrap that value. We'll store it in a local constant castArray.

// we're just adding: let castArray = spectreDictionary["cast"]

if let spectreDictionary = movieDictionary["Spectre"], let cast = spectreDictionary["cast"] // ...

so that will get us the cast names in an array. We want to pull out the lead actor and it's at index 0, we know how to do that! Once we're inside the if let statement, we can just use the cast variable:

solution.swift
if let spectreDictionary = movieDictionary["Spectre"], let cast = spectreDictionary["cast"] {
    leadActor = cast[0]
}

If any of that doesn't make sense, I really suggest going back and watching the videos again!

If you're on the Swift 2 track, you should be working on some projects soon that will use these concepts.

Cheers :beers:

-Greg

David Alberto Fuentes Ortiz
David Alberto Fuentes Ortiz
1,593 Points

THANK YOU SO MUCH NOW I UNDERSTAND IT BETTER. THANKS.

David Alberto Fuentes Ortiz
David Alberto Fuentes Ortiz
1,593 Points

ALSO CAN YOU HELP ME FIND PROJECTS TO PRACTICE PLEASEE/......

Greg Kaleka
Greg Kaleka
39,021 Points

The site HackingWithSwift comes to mind. You might want to create a separate post to ask the community for some other ideas.