Separating multiple if conditions with commas in Swift
Solution 1:
Actually the result is not the same. Say that you have 2 statements in an if and && between them. If in the first one you create a let using optional binding, you won't be able to see it in the second statement. Instead, using a comma, you will.
Comma example:
if let cell = tableView.cellForRow(at: IndexPath(row: n, section: 0)), cell.isSelected {
//Everything ok
}
&& Example:
if let cell = tableView.cellForRow(at: IndexPath(row: n, section: 0)) && cell.isSelected {
//ERROR: Use of unresolved identifier 'cell'
}
Hope this helps.
Solution 2:
In Swift 3, the where
keyword in condition clauses were replaced by a comma instead.
So a statement like if 1 == 1, 2 == 2 {}
is saying "if 1 equals 1 where 2 equals 2..."
It's probably easiest to read a conditional statement with an &&
instead of a ,
, but the results are the same.
You can read more about the details of the change in Swift 3 in the Swift Evolution proposal: https://github.com/apple/swift-evolution/blob/master/proposals/0099-conditionclauses.md