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 trial

iOS Swift Functions and Optionals Optionals What is an Optional?

Why doesn't the 'culprit' constant in the if let conditional require a "==" (is equal to)?

So the corret code is: if let culprit = findApt("101") { println("Apt Found: (culprit)") }

but I intuitively thought it would be: if let culprit == findApt("101") { println("Apt Found: (culprit)") }

3 Answers

Stephen McMillan
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Stephen McMillan
iOS Development with Swift Techdegree Graduate 33,994 Points

== isn't required because we are assigning 'let culprit' to the return value of the function findApt() . The IF statement is there to make sure we are not trying unwrap(!) an optional that is nil.

Cool and thanks, so, because of this, this can be considered different than a stand-alone if then conditional?

Ed Williams
Ed Williams
2,969 Points

They are both IF conditionals, but this one is different in the way that it is checking if the constant is assigned a value, whereas with '==' we are checking if the two variables on either side of the comparison operator are equal.