操作符
&&(_:
&&(_:_:)
對兩個布爾值執行邏輯與運算。
聲明
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 &&(_:_:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。