Swift 3: Joining parts of a multi-clause condition
Before Swift 3, when you wanted to join parts of a multi-clause condition that included optional unwrapping you could make use of the where
if let val = anOptional where val == someNumber { … }
This would have allowed to optionally unwrap the anOptional
variable and in the same conditional check, that the unwrapped value was equal to someNumber
.
As of Swift 3, if you want to to write a multi-clause condition, that includes optional unwrapping you can no longer use a where
clause. In order to join parts of a multi-clause condition now, you have to separate the clauses with a comma.
if let val = anOptional, val == someNumber { … }
The separate clauses will be evaluated one after the other and the entire multi-clause condition is only true if all individual conditions are true also. This makes joining parts of a multi clause condition including optional unwrapping with commas equivalent to a logical AND operation.
A logical OR multi-clause condition with optional unwrapping does not exist, because it wouldn’t make sense, as this would allow the if condition to be true even if the optional unwrapping failed (if any of the other clauses evaluated to true) This would defeat the purpose of optional unwrapping as the if-let
statement should only ever enter the body if the optional value could be successfully unwrapped.