操作符
||(_:
||(_:_:)
对两个布尔值执行逻辑或运算。
声明
static func || (lhs: Bool, rhs: @autoclosure () throws -> Bool) rethrows -> Bool
参数
lhs操作的左侧。
rhs操作的右侧。
详述
逻辑 OR 运算符 (||) 组合两个布尔值,如果其中至少一个值为 true,则返回 true。如果两个值都是 false ,则运算符返回 false 。
此运算符使用短路评估:首先评估左侧(lhs),仅当 lhs 评估为 false 时才评估右侧(rhs)。例如:
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"
在此示例中,lhs 测试 error 是否为空字符串。 || 运算符的评估是以下之一:
-
当
error为空字符串时,lhs评估为true并且不评估rhs,跳过对majorErrors.contains(_:)的调用。操作的结果是true。 -
当
error不是空字符串时,lhs的计算结果为false并且rhs被计算。评估rhs的结果是||操作的结果。
可用版本
iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+
相关用法
- Swift Bool !(_:)用法及代码示例
- Swift Bool random()用法及代码示例
- Swift Bool init(booleanLiteral:)用法及代码示例
- Swift Bool random(using:)用法及代码示例
- Swift Bool &&(_:_:)用法及代码示例
- Swift Bool toggle()用法及代码示例
- Swift Bool用法及代码示例
- Swift BooleanLiteralType用法及代码示例
- Swift BinaryInteger quotientAndRemainder(dividingBy:)用法及代码示例
- Swift BinaryInteger trailingZeroBitCount用法及代码示例
- Swift BinaryInteger init(clamping:)用法及代码示例
- Swift BinaryInteger <<=(_:_:)用法及代码示例
- Swift BinaryInteger !=(_:_:)用法及代码示例
- Swift BinaryInteger /(_:_:)用法及代码示例
- Swift BinaryInteger init(truncatingIfNeeded:)用法及代码示例
- Swift BinaryInteger ==(_:_:)用法及代码示例
- Swift BinaryFloatingPoint binade用法及代码示例
- Swift BidirectionalCollection subscript(_:)用法及代码示例
- Swift BidirectionalCollection last(where:)用法及代码示例
- Swift BidirectionalCollection lastIndex(where:)用法及代码示例
- Swift BinaryInteger init(_:)用法及代码示例
- Swift BinaryInteger %=(_:_:)用法及代码示例
- Swift BinaryFloatingPoint random(in:using:)用法及代码示例
- Swift BinaryInteger &=(_:_:)用法及代码示例
- Swift BidirectionalCollection lastIndex(of:)用法及代码示例
注:本文由纯净天空筛选整理自apple.com大神的英文原创作品 Bool ||(_:_:)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
