Classification by Arity and Fixity
Operators are categorized by the number of operands they accept (arity) and their position relative to those operands (fixity).- Unary Operators: Operate on a single operand.
- Prefix: Placed immediately before the operand without whitespace.
- Binary Operators: Operate on two operands. They are always infix, placed between the two operands. Swift requires consistent whitespace on both sides of a binary operator to resolve ambiguity.
- Ternary Operators: Operate on three operands. Swift implements a single ternary conditional operator.
Core Operator Categories
Assignment Operator The assignment operator (=) initializes or mutates a value. Unlike in C or Objective-C, the assignment operator in Swift does not return a value. This design prevents accidental assignment within conditional statements.
+, -, *, /, %) perform mathematical operations. Swift prevents silent arithmetic overflow; operations that exceed the bounds of the type will trigger a runtime trap.
&+, &-, &*).
Comparison and Identity Operators
Comparison operators evaluate to a Bool. Identity operators (===, !==) check whether two object references point to the exact same instance in memory, which is distinct from value equality (==).
??) unwraps an optional type if it contains a value, or falls back to a default value if the optional is nil. It utilizes short-circuit evaluation; the right-hand operand is not evaluated if the left-hand operand is non-nil.
Range and ClosedRange types.
- Closed Range (
...): Defines a range running from the lower bound to the upper bound, inclusive. - Half-Open Range (
..<): Defines a range running from the lower bound to the upper bound, exclusive of the upper bound. - One-Sided Range: Omits either the lower or upper bound to create a partial range.
- Logical: (
!,&&,||) Operate on boolean logic. The&&and||operators employ short-circuit evaluation. - Bitwise: (
~,&,|,^,<<,>>) Manipulate the raw data bits within a data structure.
Precedence and Associativity
Infix operators are governed by precedence and associativity rules, which dictate the parsing order of complex expressions without explicit parentheses.- Precedence: Determines which operator binds tighter to its operands (e.g., multiplication has higher precedence than addition).
- Associativity: Determines how operators of the exact same precedence are grouped (left-associative or right-associative).
Custom Operators
Swift allows the declaration of custom operators using theoperator keyword. Custom operators must be declared at the global scope and assigned a fixity (prefix, infix, or postfix).
Infix operators can optionally be assigned to a precedencegroup to define their evaluation order relative to other operators. If a precedence group is not explicitly specified, Swift automatically assigns the custom infix operator to a default precedence group (DefaultPrecedence), which has a precedence immediately higher than the ternary conditional operator.
Tired of Poor Swift Skills? Fix That With Deep Grasping!Learn More





