操作符
&&(_:
&&(_:_:)
对两个布尔值执行逻辑与运算。
声明
static func && (lhs: Bool, rhs: @autoclosure () throws -> Bool) rethrows -> Bool
参数
lhs操作的左侧。
rhs操作的右侧。
详述
逻辑 AND 运算符 (&&) 组合两个布尔值,如果两个值都是 true,则返回 true。如果任一值是 false ,则运算符返回 false 。
此运算符使用短路评估:首先评估左侧(lhs),仅当 lhs 评估为 true 时才评估右侧(rhs)。例如:
let measurements = [7.44, 6.51, 4.74, 5.88, 6.27, 6.12, 7.76]
let sum = measurements.reduce(0, +)
if measurements.count > 0 && sum / Double(measurements.count) < 6.5 {
print("Average measurement is less than 6.5")
}
// Prints "Average measurement is less than 6.5"
在此示例中,lhs 测试 measurements.count 是否大于零。 && 运算符的评估是以下之一:
-
当
measurements.count等于 0 时,lhs的计算结果为false并且不计算rhs,从而防止表达式sum / Double(measurements.count)中出现 divide-by-zero 错误。操作的结果是false。 -
当
measurements.count大于零时,lhs的计算结果为true并且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 ||(_:_:)用法及代码示例
- Swift Bool init(booleanLiteral:)用法及代码示例
- Swift Bool random(using:)用法及代码示例
- 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 &&(_:_:)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
