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?

if culprint != nil

Why not just check if culprint is nil?

if culprint != nil{
    println("Apt found: \(culprint!)")
}
Austin Murtha
Austin Murtha
4,843 Points

I notice this often as well. Interested as well.

1 Answer

I think the "if let" syntax comes in handy for more complex situations. Imagine if you had a long switch or if/else block that uses the optional value. You have to make sure to unwrap it in every case if you use the "if != nil" solution. The "if let" unwraps and stores it in the constant so you can just reference the constant as needed within your block.

When you're using optionals that store UIViews, the compiler doesn't know what properties a wrapped object has, so you'll end up with a bunch of errors all through your block telling you your label doesn't have ".text" when what's really going on is you needed to use "myLabel!.text"