44

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.

Luca Angeletti
  • 58,465
  • 13
  • 121
  • 148
Marcus Leon
  • 55,199
  • 118
  • 297
  • 429

3 Answers3

82

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 optional bindings 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.

Luca Angeletti
  • 58,465
  • 13
  • 121
  • 148
33

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"
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
  • 3
    I myself never knew about the `where` clause until looking it up in the docs, you learn something new every day... :) – Joachim Isaksson Feb 16 '16 at 14:47
  • 10
    [`where` clauses are no longer used like that in Swift 3](https://github.com/apple/swift-evolution/blob/master/proposals/0099-conditionclauses.md), just replace `where` with a comma instead. – Flimm Sep 28 '16 at 10:17
  • Why would you use the comma approach versus the conditional and statement? – Andrew Tuzson Jan 20 '18 at 11:22
  • 3
    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) – GetSwifty Feb 02 '18 at 20:53
  • @PeejWeej Yes, with Swift 4 you can't use where, instead use a comma. – ThomasW Mar 28 '18 at 06:31
  • 1
    Is using the comma the same as using && i.e. and? if let a = optA && let b = optB && let c = optC { – mretondo Jul 14 '18 at 18:44
  • 7
    @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 }` – leanne Jul 20 '18 at 20:52
2

Triggered by a code fragment that was suggested by ChatGPT I played with the comma separator (",") as an equivalent of the common AND separator ("&&"). They seem to be equivalent, although I could not find it in the Swift documentation. It is documented for the use in optional binding though (https://docs.swift.org/swift-book/documentation/the-swift-programming-language/thebasics/#Optional-Binding)

let a = 1
let b = 2
let c = 3

if a == 1, c == 3 {
    print("a == 1, c == 3")
}
// prints "a == 1, c == 3"

if a == 1, b == 2, c == 3 {
    print("a == 1, b == 2, c == 3")
}
// prints "a == 1, b == 2, c == 3"

if a == 1, b == 1, c == 1 {
    print("a == 1, b == 1, c == 1")
}
else {
    print("not a == 1, b == 1, c == 1")
}
// prints "not a == 1, b == 1, c == 1"