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 trialStephan Porsche
2,864 PointsI'm getting the error "Initializer for conditional binding must have Optional type, not 'String" in the following task.
See my code here:
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 movie = movieDictionary["Spectre"], let cast = movie["cast"], let actor = cast[0] {
leadActor = actor
}
4 Answers
Marina Alenskaja
9,320 PointsHi Stephan
Seems like you are trying to get a value out of the key "cast" twice - here is the correct approach:
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]
}
Kantimoy Sur
iOS Development with Swift Techdegree Student 22,499 PointsHi Alexandru Stefanescu , Actually you have put (curly braces) i.e. "{" , whereas it should be a (coma) i.e. "," . and also the questions asks you to store the result in a variable "leadActor" which is already declared, if you see to the top - ( var leadActor )
So code is ``` html if let movie = movieDictonary[" Spectre "] , if let movieCast = movie[" cast "] { leadActor = movieCast[0] }
Alexandru Stefanescu
2,195 PointsThanks, I should have paid more attention :)
Emmanuoel Haroutunian
13,532 PointsI'm a bit confused. I get that we need to check if the key returns a value, but couldn't the same logic apply to the array inside cast? What if it was an empty array and position 0 was nil?
Jake Adams
1,608 PointsYou are correct. If the array was empty, you'd get an "array index out of range" exception. However, I don't think a 'let' optional binding is the answer. You'd have to explicitly check the array count inside the if clause
let movieDictionary = ["Spectre": ["cast": Array<String>()]]
var leadActor: String?
if let movie = movieDictionary["Spectre"], let cast = movie["cast"] {
leadActor = cast.count > 0 ? cast[0] : nil
}
Alexandru Stefanescu
2,195 PointsHello all :),
any idea why this is not accepted?
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"] {
if let movieCast = movie["cast"] {
let actor = movieCast[0]
}
}
James Killeen
Courses Plus Student 2,207 PointsHi Alexandru, I believe your problem here is the addition of {} instead of comma, after the ["Spectre"]. Then on your second line, you have used an "if let" instead of just a "let"
Stephan Porsche
2,864 PointsStephan Porsche
2,864 PointsYou're dead right! What was I thinking... Thanks alot. :)