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 trialshane reichefeld
Courses Plus Student 2,232 PointsIm confused on what to assign to what value?
Help?
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 name = movieDictionary["Daniel Craig"], let leadActor = name["cast"] {
}
1 Answer
Reed Carson
8,306 PointsI can help you with opening the dictionary, which can be extremely confusing. Think of it as opening it layer by layer. First you want the first layer which is the movie Spectre. You can use
if let movie = movieDictionary["Spectre"] {
}
this puts you in that first layer. Now you want to get to the second layer. You use the temporary constant you just made ("movie") which stands for the first layer.
if let cast = movie["cast"] {
}
now you are in the second layer, cast, which contains an array of the actors stored as strings. You want the actor at index 0, the first position in the array. And you want to assign that to leadActor
leadActor = cast[0]
Now, whatever is at index 0 of cast has been assigned to leadActor
if let movie = movieDictionary["Spectre"] {
if let cast = movie["cast"] {
leadActor = cast[0]
}
}
I'd say think of it like peeling an onion but I hate that metaphor and onions dont contain strings