if, let, let, where statements

Swift 1.2 came with new possibilities for if statements. One big improvement is using multiple lets in one if statement, using the first let declaration in the second:

if let car = createCar(), let color = car.color {
    if color == UIColor.redColor() {

    }
}

Even better is combing it with where:

if let car = createCar(), let color = car.color where color == UIColor.redColor() {

}

This results in clean code and less if statements. Your code will be more readable, which should make your colleagues a bit happier!

All posts in this category

 

Antoine van der Lee

Dutch iOS developer at Triple. Developed apps like Buienradar, Videoland and Pop the Dots.

 
Follow on Feedly