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

Referencing array index inside optionally unwrapped dictionary

It turns out that its a little more complicated than I thought...

I thought it might be something like:

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

However I'm stuck on this Bummer:

Bummer! Make sure that you're using array subscript syntax to get the value for the lead actor

But after doing a some research I came across several examples that uses a question mark, so I tried:

if let dict = movieDictionary["Spectre"] {
  if let leadActor = dict["cast"]?[0] {} 

More research brought me to this stackoverflow thread:


If you read through the first answer in that thread (about "3 possible things here that you might be conflating")

and you'll see that things are a bit more complicated in terms of options than Pasan hinted at in any of the videos.

I found very few examples anywhere on the Internet where a value for an array inside a dictionary was referenced using an array index (using the optional-oriented "if let").

Note: If I drop the 'if' from the second 'if let' I get a different error:

Bummer! Make sure that you're using optional binding to unwrap the value obtained from using the cast key

..so I'm thinking 'if let' is necessary on both lines.

Also tried:

if let dict = movieDictionary["Spectre"],
  if let leadActor = dict!["cast[0]"] {

..which gave me this Bummer:

Bummer! Make sure that you're using optional binding to unwrap the value obtained from using the Spectre key

..which just seems really wrong.

So I guess I'm stuck for now...

7 Answers

Marina Alenskaja
Marina Alenskaja
9,320 Points

Hi James!

You a missing another if let - right now you are only accessing the movie and then the actor directly, but you need to access the "cast" key first:

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]

Good answer :)

By the way, I am wondering if you guys would be interested in joining a group I created to practice by making simple apps. This helps us learn how to collaborate in groups by using GitHub, and it also helps us extend our current skills. Right now I am working on a simple app that looks like this Test

Marina Alenskaja & james white

if you are interested in collaborating in this app, please send me your e-mail and I will add you to the group.

Marina I just sent you an invite, look for an e-mail from Slack.


As Marina pointed out, you are missing another let statement.

The beautiful thing about Swift 2.0 is that you don't necessary need to use the "let" keyword. It could be even easier, but for this challenge you would have to use the "let" keyword. Here is an example of what I mean.

if let movie = movieDictionary["Spectre"], movieCast = movie["cast"] {
    leadActor = movieCast[0]
// Notice how I didn't use "let movieCast " in the chain

But to pass this challenge you would do it as Marina Alenskaja showed you.

Good luck

Thanks Martina and Jhoan!

Marina's solution seems easier to follow, but I'm glad Jhoan was able to show how to finesse the code to only use one "if let".

durul dalkanat
durul dalkanat
62,690 Points

Jhoan Arango Can I join group ?

Of course, I need an e-mail where I can send you the invite.

Check your e-mail for a slack invite.

Josh Minor
Josh Minor
2,947 Points

jhoan Arango, you still doing simple app practice?

Yes ! Send me your e-mail and Ill send you an invite