Article

Operator Declarations

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 (Swift 4.1).

Table 1

Prefix operators

Operator

Description

!

Logical NOT

~

Bitwise NOT

+

Unary plus

-

Unary minus

..<

Half-open range

...

Closed range

Table 2

Postfix operators

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 DefaultPrecedence precedence group. DefaultPrecedence has no associativity and a precedence immediately higher than TernaryPrecedence.

Table 3

Infix operators

Operator

Description

Associativity

Precedence group

<<

Bitwise left shift

None

BitwiseShiftPrecedence

>>

Bitwise right shift

None

BitwiseShiftPrecedence

*

Multiply

Left associative

MultiplicationPrecedence

/

Divide

Left associative

MultiplicationPrecedence

%

Remainder

Left associative

MultiplicationPrecedence

&*

Multiply, ignoring overflow

Left associative

MultiplicationPrecedence

&

Bitwise AND

Left associative

MultiplicationPrecedence

+

Add

Left associative

AdditionPrecedence

-

Subtract

Left associative

AdditionPrecedence

&+

Add with overflow

Left associative

AdditionPrecedence

&-

Subtract with overflow

Left associative

AdditionPrecedence

|

Bitwise OR

Left associative

AdditionPrecedence

^

Bitwise XOR

Left associative

AdditionPrecedence

..<

Half-open range

None

RangeFormationPrecedence

...

Closed range

None

RangeFormationPrecedence

is

Type check

Left associative

CastingPrecedence

as, as?, and as!

Type cast

Left associative

CastingPrecedence

??

Nil coalescing

Right associative

NilCoalescingPrecedence

<

Less than

None

ComparisonPrecedence

<=

Less than or equal

None

ComparisonPrecedence

>

Greater than

None

ComparisonPrecedence

>=

Greater than or equal

None

ComparisonPrecedence

==

Equal

None

ComparisonPrecedence

!=

Not equal

None

ComparisonPrecedence

===

Identical

None

ComparisonPrecedence

!==

Not identical

None

ComparisonPrecedence

~=

Pattern match

None

ComparisonPrecedence

&&

Logical AND

Left associative

LogicalConjunctionPrecedence

||

Logical OR

Left associative

LogicalDisjunctionPrecedence

?:

Ternary conditional

Right associative

TernaryPrecedence

=

Assign

Right associative

AssignmentPrecedence

*=

Multiply and assign

Right associative

AssignmentPrecedence

/=

Divide and assign

Right associative

AssignmentPrecedence

%=

Remainder and assign

Right associative

AssignmentPrecedence

+=

Add and assign

Right associative

AssignmentPrecedence

-=

Subtract and assign

Right associative

AssignmentPrecedence

<<=

Left bit shift and assign

Right associative

AssignmentPrecedence

>>=

Right bit shift and assign

Right associative

AssignmentPrecedence

&=

Bitwise AND and assign

Right associative

AssignmentPrecedence

|=

Bitwise OR and assign

Right associative

AssignmentPrecedence

^=

Bitwise XOR and assign

Right associative

AssignmentPrecedence

See Also

Programming Tasks

Input and Output

Print values to the console, read from and write to text streams, and use command line arguments.

Debugging and Reflection

Fortify your code with runtime checks, and examine your values' runtime representation.

Key-Path Expressions

Use key-path expressions to access properties dynamically.

Manual Memory Management

Allocate and manage memory manually.

Type Casting and Existential Types

Perform casts between types or represent values of any type.

C Interoperability

Use imported C types or call C variadic functions.