Work with prefix, postfix, and infix operators.
Overview
The tables below list the operators declared in Swift. Table 1 lists the prefix operators, Table 2 lists the postfix operators, and Table 3 lists the infix operators, including their associativity and precedence group. For more information about operation declarations, see Operator Declaration in The Swift Programming Language.
Operator | Description |
---|---|
| Logical NOT |
| Bitwise NOT |
| Unary plus |
| Unary minus |
| Half-open range |
| Closed range |
Operator | Description |
---|---|
| Range |
The infix operators are grouped below by precedence group in decreasing order of precedence. If you declare a new operator without specifying a precedence group, it is a member of the Default
precedence group. Default
has no associativity and a precedence immediately higher than Ternary
.
Operator | Description | Associativity | Precedence group |
---|---|---|---|
| Bitwise left shift | None |
|
| Bitwise right shift | None |
|
| Multiply | Left associative |
|
| Divide | Left associative |
|
| Remainder | Left associative |
|
| Multiply, ignoring overflow | Left associative |
|
| Bitwise AND | Left associative |
|
| Add | Left associative |
|
| Subtract | Left associative |
|
| Add with overflow | Left associative |
|
| Subtract with overflow | Left associative |
|
| Bitwise OR | Left associative |
|
| Bitwise XOR | Left associative |
|
| Half-open range | None |
|
| Closed range | None |
|
| Type check | Left associative |
|
| Type cast | Left associative |
|
| Nil coalescing | Right associative |
|
| Less than | None |
|
| Less than or equal | None |
|
| Greater than | None |
|
| Greater than or equal | None |
|
| Equal | None |
|
| Not equal | None |
|
| Identical | None |
|
| Not identical | None |
|
~= | Pattern match | None |
|
| Pointwise equal | None |
|
| Pointwise not equal | None |
|
| Pointwise less than | None |
|
| Pointwise less than or equal | None |
|
| Pointwise greater than | None |
|
| Pointwise greater than or equal | None |
|
| Logical AND | Left associative |
|
| Logical OR | Left associative |
|
| Ternary conditional | Right associative |
|
| Assign | Right associative |
|
| Multiply and assign | Right associative |
|
| Divide and assign | Right associative |
|
| Remainder and assign | Right associative |
|
| Add and assign | Right associative |
|
| Subtract and assign | Right associative |
|
| Left bit shift and assign | Right associative |
|
| Right bit shift and assign | Right associative |
|
| Bitwise AND and assign | Right associative |
|
| Bitwise OR and assign | Right associative |
|
| Bitwise XOR and assign | Right associative |
|