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 trialJames Stott
11,560 PointsHow does `let name = value` resolve to a boolean?
In the 'What is an optional' video the following code is displayed:
if let culprit = findApt("102") {
println("Apt Found: \(culprit)")
}
where findApt()
returns an optional
How does this work? Does the let
command return a boolean when invoked?
2 Answers
landonferrier
25,097 PointsJames, remember, when the compiler compiles an if statement, there are only two outcomes of the value in the if statement, true or false. In the case of the example code you have provided there are two outcomes when that code in compiled. If the findApt() method returns a value, any value, it will be true and executed. If the findApt() method doesn't return any value, the if will not pass and the code inside the statement will never be executed.
If you have any questions, feel free to ask!
sieder villareal
4,016 Pointsit does return a boolean if you think about it, because the code you posted is a conditional and it is expected that it will return true or false according to the condition. let command means "culprit" it is a constant, immutable, value can't be change.
Jeanne Merle
3,390 PointsJeanne Merle
3,390 PointsIn fact, it means "If I can assign a value tu culprit, THEN it's true" , doesn't it ?
Edward Ulpindo
700 PointsEdward Ulpindo
700 PointsThat makes sense. But if all you need is a non-nil value to evaluate the conditional, why does if findApt() throw an error (as it did in Amit's lesson)?