Performs a logical OR operation on two Boolean values.
SDK
- Xcode 8.0+
Framework
- Swift Standard Library
Declaration
Parameters
lhs
The left-hand side of the operation.
rhs
The right-hand side of the operation.
Discussion
The logical OR operator (||
) combines two Boolean values and returns true
if at least one of the values is true
. If both values are false
, the operator returns false
.
This operator uses short-circuit evaluation: The left-hand side (lhs
) is evaluated first, and the right-hand side (rhs
) is evaluated only if lhs
evaluates to false
. For example:
let majorErrors: Set = ["No first name", "No last name", ...]
let error = ""
if error.isEmpty || !majorErrors.contains(error) {
print("No major errors detected")
} else {
print("Major error: \(error)")
}
// Prints "No major errors detected"
In this example, lhs
tests whether error
is an empty string. Evaluation of the ||
operator is one of the following:
When
error
is an empty string,lhs
evaluates totrue
andrhs
is not evaluated, skipping the call tomajor
. The result of the operation isErrors .contains(_:) true
.When
error
is not an empty string,lhs
evaluates tofalse
andrhs
is evaluated. The result of evaluatingrhs
is the result of the||
operation.