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 trialAkshay Jain
2,576 PointsNil Values in Collections
Hi everyone, this is my first time posting a question so I am sorry if this looks weird when it posts.
The purpose of the challenge was to retrieve the first actor at position 0 in the dictionary. The code I used in Xcode worked however in the treehouse challenge it says "make sure you are retrieving the string at index 0 in the nested array." Can someone please explain why my code is wrong if it works in Xcode?
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 leadActor = movieDictionary["Spectre"], let mainActor = leadActor["cast"] { print(mainActor[0]) }
3 Answers
Matt Skelton
4,548 PointsHey Akshay,
I recall the challenge, though I can't currently find it within the library so I can't check the specifics of the task. If you can remind me of where exactly it is I'd be happy to look a little deeper.
In the meantime, I'm going to speculate that the problem may be around your leadActor variable. If I recall correctly, the challenge wants you to dig out the value of Daniel Craig (like you have done) and assign it to the leadActor variable. However, at the moment you are assigning Daniel Craig to your new variable, mainActor.
As I say, this is purely a matter of me trying to remember the task from muscle memory, so point me in the right direction for the task if you need more help.
In order to format code nicely, you want to make use of this character `. That's the one above '~' on your keyboard, not the standard apostrophe! You need three of them before your snippet, and three after. Take a look at the Markdown Cheatsheet just below the comment box for a clearer example.
Good luck!
Akshay Jain
2,576 PointsHey Matt thanks for your response! That was the main issue with my code; your answer really helped! Good luck to you to man!
Akshay Jain
2,576 PointsAlso, how do I post code so its in the black background so it doesn't look weird when I post questions?
Collin Cannavo
2,494 PointsYou just need to check the box that says "show my code" (or something like that) before you post it