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 Enumerations and Optionals in Swift Introduction to Optionals Nil Values in Collections

Ken Gerlinger
Ken Gerlinger
2,898 Points

Hello am currently losing me mind on this challenge (array inside nested dictionary).

I would like to solve it on my own, but I need some advices. Could you explain to me how to access or call a String which is inside an array that it self is inside a nested dictionary? (e.g. - movieDictionary = [String: [String: [String]]] - )

Any other advices are appreciated!



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

var leadActor: String = ""

// Enter code below

2 Answers

Heidi Puk Hermann
Heidi Puk Hermann
33,352 Points


you need to access each array/dictionary at a time, starting from the outside and going in. (Really hope this will make sense :) )

Imagine that you are building an app where people can store all their clothes. Each person has their own row, which contains a number of dictionaries - one for each type of clothes. The types of clothes are sorted by colour (just to have the same type of structure as you have...).

var clothesDictionary = [
   "person1": ["shirts": ["blue", "black"], "pants": ["black", "white"]], 
   "person2": ["shirts": ["pink", "red"], "pants": ["cowboy", "black"]]

/* Access the colour of the second pair of pants for person2... 
    - Since I don't know if I have a person2, I have to check if he exists first.
    - Then check if he has any pants, and then I can get out the colors.*/

if let person = clothesDictionary["person2"], let pants = person["pants"] {
   var secondPantsColor: String = pants[1]

Hope that it can help you :)

Ken Gerlinger
Ken Gerlinger
2,898 Points

Thank you so much !

Perfectly explained

Heidi Puk Hermann
Heidi Puk Hermann
33,352 Points

Thank you for the feedback ! - It was a fun exercise for me to try and explain it, without giving away the answer, and I'm so happy it worked :D