Swift if statement - multiple conditions separated by commas?

30,815

Solution 1

Yes when you write

if let a = optA, let b = optB, let c = optC {

}

Swift does execute the body of the IF only if all the assignments are properly completed.

More

Another feature of this technique: the assignments are done in order.

So only if a value is properly assigned to a, Swift tries to assign a value to b. And so on.

This allows you to use the previous defined variable/constant like this

if let a = optA, let b = a.optB {

}

In this case (in second assignment) we are safely using a because we know that if that code is executed, then a has been populated with a valid value.

Solution 2

Yes. Swift: Documentation: Language Guide: The Basics: Optional Binding says:

You can include as many optional bindings and Boolean conditions in a single if statement as you need to, separated by commas. If any of the values in the optional bindings are nil or any Boolean condition evaluates to false, the whole if statement’s condition is considered to be false. The following if statements are equivalent:

if let firstNumber = Int("4"), let secondNumber = Int("42"), firstNumber < secondNumber && secondNumber < 100 {
    print("\(firstNumber) < \(secondNumber) < 100")
}   
// Prints "4 < 42 < 100"

if let firstNumber = Int("4") {
    if let secondNumber = Int("42") {
        if firstNumber < secondNumber && secondNumber < 100 {
            print("\(firstNumber) < \(secondNumber) < 100")
        }   
    }   
}   
// Prints "4 < 42 < 100"
Share:
30,815
Marcus Leon
Author by

Marcus Leon

Director Clearing Technology, Intercontinental Exchange. Develop the clearing systems that power ICE/NYSE's derivatives markets.

Updated on June 27, 2020

Comments

  • Marcus Leon
    Marcus Leon almost 4 years

    Looking at a Swift example:

    if let sourceViewController = sender.sourceViewController as? MealViewController, meal = sourceViewController.meal {
        ...
    }
    

    The doc states:

    ... the code assigns that view controller to the local constant sourceViewController, and checks to see if the meal property on sourceViewController is nil.

    Question: Does Swift let you have multiple conditions in your if statement when separated by commas (as in this example with the comma after MealViewController)?

    Haven't seen this in the docs.

  • Joachim Isaksson
    Joachim Isaksson about 8 years
    I myself never knew about the where clause until looking it up in the docs, you learn something new every day... :)
  • Flimm
    Flimm over 7 years
    where clauses are no longer used like that in Swift 3, just replace where with a comma instead.
  • Andrew Tuzson
    Andrew Tuzson over 6 years
    Why would you use the comma approach versus the conditional and statement?
  • GetSwifty
    GetSwifty over 6 years
    FYI, where clauses in if statements are no longer valid in Swift 4+. I believe it was removed in Swift 3 but I can't find a reference)
  • ThomasW
    ThomasW about 6 years
    @PeejWeej Yes, with Swift 4 you can't use where, instead use a comma.
  • mretondo
    mretondo almost 6 years
    Is using the comma the same as using && i.e. and? if let a = optA && let b = optB && let c = optC {
  • leanne
    leanne almost 6 years
    @mretondo, in Swift 4.1, at least, your example gives the error Expected ',' joining parts of a multi-clause condition, along with the suggestion to fix by replacing the && with ,. Using the comma is a way to ensure you don't have an optional, while using && indicates a boolean test. To use && you'd have to say this instead: if optA != nil && optB != nil && optC != nil {. Alternatively, if you need those unwrapped variables inside the block, you could use commas again: if let a = optA, a>1, let b = optB, b>1, let c = optC, c>1 { return a + b + c }