- Mastering macOS Programming
- Stuart Grimshaw
- 121字
- 2025-04-04 19:10:24
Logical operators
Swift's logical NOT, AND, and OR operators are the same as most C-based languages:
let saturday = true
let raining = false
let christmas = true
let b1 = !saturday // false
let b2 = saturday && raining // false
let b3 = saturday || raining // true
Multiple operators are allowed, and are left-associative, that is, sub-expressions are evaluated from left to right:
let b4 = (saturday || raining) && !christmas
In the preceding code, (saturday || raining) is evaluated first, and the result of that evaluation goes on to be evaluated with && !christmas. The parentheses as added are permissible, which can add clarity to your code, although they make no difference here to the code's outcome.