Swift if statement - multiple conditions separated by commas?
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 arenil
or any Boolean condition evaluates tofalse
, the wholeif
statement’s condition is considered to befalse
. The followingif
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"
Marcus Leon
Director Clearing Technology, Intercontinental Exchange. Develop the clearing systems that power ICE/NYSE's derivatives markets.
Updated on June 27, 2020Comments
-
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 about 8 yearsI myself never knew about the
where
clause until looking it up in the docs, you learn something new every day... :) -
Flimm over 7 years
where
clauses are no longer used like that in Swift 3, just replacewhere
with a comma instead. -
Andrew Tuzson over 6 yearsWhy would you use the comma approach versus the conditional and statement?
-
GetSwifty over 6 yearsFYI,
where
clauses inif
statements are no longer valid in Swift 4+. I believe it was removed in Swift 3 but I can't find a reference) -
ThomasW about 6 years@PeejWeej Yes, with Swift 4 you can't use where, instead use a comma.
-
mretondo almost 6 yearsIs using the comma the same as using && i.e. and? if let a = optA && let b = optB && let c = optC {
-
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 }