Operators are special symbols that perform a computation for one or more values. They are either unary, binary, or ternary.

Unary operators perform an operation for a single value. The unary operator symbol appears before the value.

Binary operators operate on two values. The binary operator symbol appears between the two values (infix).

Ternary operators operate on three values. The first operator symbol appears between the first and second value, the second operator symbol appears between the second and third value (infix).
The binary assignment operator =
can be used
to assign a new value to a variable.
It is only allowed in a statement and is not allowed in expressions.
1var a = 12a = 23// `a` is `2`456var b = 37var c = 489// Invalid: The assignment operation cannot be used in an expression.10a = b = c1112// Instead, the intended assignment must be written in multiple statements.13b = c14a = b
Assignments to constants are invalid.
1let a = 12// Invalid: Assignments are only for variables, not constants.3a = 2
The lefthand side of the assignment operand must be an identifier. For arrays and dictionaries, this identifier can be followed by one or more index or access expressions.
1// Declare an array of integers.2let numbers = [1, 2]34// Change the first element of the array.5//6numbers[0] = 378// `numbers` is `[3, 2]`
1// Declare an array of arrays of integers.2let arrays = [[1, 2], [3, 4]]34// Change the first element in the second array5//6arrays[1][0] = 578// `arrays` is `[[1, 2], [5, 4]]`
1let dictionaries = {2true: {1: 2},3false: {3: 4}4}56dictionaries[false][3] = 078// `dictionaries` is `{9// true: {1: 2},10// false: {3: 0}11//}`
The forceassignment operator (<!
) assigns a resourcetyped value
to an optionaltyped variable if the variable is nil.
If the variable being assigned to is nonnil,
the execution of the program aborts.
The forceassignment operator is only used for resource types.
The binary swap operator <>
can be used
to exchange the values of two variables.
It is only allowed in a statement and is not allowed in expressions.
1var a = 12var b = 23a <> b4// `a` is `2`5// `b` is `1`67var c = 389// Invalid: The swap operation cannot be used in an expression.10a <> b <> c1112// Instead, the intended swap must be written in multiple statements.13b <> c14a <> b
Both sides of the swap operation must be variable, assignment to constants is invalid.
1var a = 12let b = 234// Invalid: Swapping is only possible for variables, not constants.5a <> b
Both sides of the swap operation must be an identifier, followed by one or more index or access expressions.
The unary pefix operator 
negates an integer:
1let a = 12a // is `1`
There are four binary arithmetic operators:
 Addition:
+
 Subtraction:

 Multiplication:
*
 Division:
/
 Remainder:
%
1let a = 1 + 22// `a` is `3`
The arguments for the operators need to be of the same type. The result is always the same type as the arguments.
The division and remainder operators abort the program when the divisor is zero.
Arithmetic operations on the signed integer types
Int8
, Int16
, Int32
, Int64
, Int128
, Int256
,
and on the unsigned integer types
UInt8
, UInt16
, UInt32
, UInt64
, UInt128
, UInt256
,
do not cause values to overflow or underflow.
1let a: UInt8 = 25523// Runtime error: The result `256` does not fit in the range of `UInt8`,4// thus a fatal overflow error is raised and the program aborts5//6let b = a + 1
1let a: Int8 = 1002let b: Int8 = 10034// Runtime error: The result `10000` does not fit in the range of `Int8`,5// thus a fatal overflow error is raised and the program aborts6//7let c = a * b
1let a: Int8 = 12823// Runtime error: The result `128` does not fit in the range of `Int8`,4// thus a fatal overflow error is raised and the program aborts5//6let b = a
Arithmetic operations on the unsigned integer types
Word8
, Word16
, Word32
, Word64
may cause values to overflow or underflow.
For example, the maximum value of an unsigned 8bit integer is 255 (binary 11111111). Adding 1 results in an overflow, truncation to 8 bits, and the value 0.
1// 11111111 = 2552// + 13// = 100000000 = 0
1let a: Word8 = 2552a + 1 // is `0`
Similarly, for the minimum value 0, subtracting 1 wraps around and results in the maximum value 255.
1// 000000002//  13// = 11111111 = 255
1let b: Word8 = 02b  1 // is `255`
Arithmetic operators are not supported for number supertypes
(Number
, SignedNumber
, FixedPoint
, SignedFixedPoint
, Integer
, SignedInteger
),
as they may or may not succeed at runtime.
1let x: Integer = 3 as Int82let y: Integer = 4 as Int834let z: Integer = x + y // Static error
Values of these types need to be cast to the desired type before performing the arithmetic operation.
1let z: Integer = (x as! Int8) + (y as! Int8)
Logical operators work with the boolean values true
and false
.

Logical NOT:
!a
This unary prefix operator logically negates a boolean:
1let a = true2!a // is `false` 
Logical AND:
a && b
1true && true // is `true`23true && false // is `false`45false && true // is `false`67false && false // is `false`If the lefthand side is false, the righthand side is not evaluated.

Logical OR:
a  b
1true  true // is `true`23true  false // is `true`45false  true // is `true`67false  false // is `false`If the lefthand side is true, the righthand side is not evaluated.
Comparison operators work with boolean and integer values.

Equality:
==
, for booleans and integersBoth sides of the equality operator may be optional, even of different levels, so it is for example possible to compare a nonoptional with a doubleoptional (
??
).11 == 1 // is `true`231 == 2 // is `false`1true == true // is `true`23true == false // is `false`1let x: Int? = 12x == nil // is `false`1let x: Int = 12x == nil // is `false`1// Comparisons of different levels of optionals are possible.2let x: Int? = 23let y: Int?? = nil4x == y // is `false`1// Comparisons of different levels of optionals are possible.2let x: Int? = 23let y: Int?? = 24x == y // is `true` 
Inequality:
!=
, for booleans and integers (possibly optional)Both sides of the inequality operator may be optional, even of different levels, so it is for example possible to compare a nonoptional with a doubleoptional (
??
).11 != 1 // is `false`231 != 2 // is `true`1true != true // is `false`23true != false // is `true`1let x: Int? = 12x != nil // is `true`1let x: Int = 12x != nil // is `true`1// Comparisons of different levels of optionals are possible.2let x: Int? = 23let y: Int?? = nil4x != y // is `true`1// Comparisons of different levels of optionals are possible.2let x: Int? = 23let y: Int?? = 24x != y // is `false` 
Less than:
<
, for integers11 < 1 // is `false`231 < 2 // is `true`452 < 1 // is `false` 
Less or equal than:
<=
, for integers11 <= 1 // is `true`231 <= 2 // is `true`452 <= 1 // is `false` 
Greater than:
>
, for integers11 > 1 // is `false`231 > 2 // is `false`452 > 1 // is `true` 
Greater or equal than:
>=
, for integers11 >= 1 // is `true`231 >= 2 // is `false`452 >= 1 // is `true`
Similar to arithmetic operators, comparison operators are also not supported for number supertypes
(Number
, SignedNumber
FixedPoint
, SignedFixedPoint
, Integer
, SignedInteger
),
as they may or may not succeed at runtime.
1let x: Integer = 3 as Int82let y: Integer = 4 as Int834let z: Bool = x > y // Static error
Values of these types need to be cast to the desired type before performing the arithmetic operation.
1let z: Bool = (x as! Int8) > (y as! Int8)
Bitwise operators enable the manipulation of individual bits of unsigned and signed integers. They're often used in lowlevel programming.

Bitwise AND:
a & b
Returns a new integer whose bits are 1 only if the bits were 1 in both input integers:
1let firstFiveBits = 0b111110002let lastFiveBits = 0b000111113let middleTwoBits = firstFiveBits & lastFiveBits // is 0b00011000 
Bitwise OR:
a  b
Returns a new integer whose bits are 1 only if the bits were 1 in either input integers:
1let someBits = 0b101100102let moreBits = 0b010111103let combinedbits = someBits  moreBits // is 0b11111110 
Bitwise XOR:
a ^ b
Returns a new integer whose bits are 1 where the input bits are different, and are 0 where the input bits are the same:
1let firstBits = 0b000101002let otherBits = 0b000001013let outputBits = firstBits ^ otherBits // is 0b00010001

Bitwise LEFT SHIFT:
a << b
Returns a new integer with all bits moved to the left by a certain number of places.
1let someBits = 4 // is 0b000001002let shiftedBits = someBits << 2 // is 0b00010000 
Bitwise RIGHT SHIFT:
a >> b
Returns a new integer with all bits moved to the right by a certain number of places.
1let someBits = 8 // is 0b000010002let shiftedBits = someBits >> 2 // is 0b00000010
For unsigned integers, the bitwise shifting operators perform logical shifting, for signed integers, they perform arithmetic shifting.
There is only one ternary conditional operator, the ternary conditional operator (a ? b : c
).
It behaves like an ifstatement, but is an expression: If the first operator value is true, the second operator value is returned. If the first operator value is false, the third value is returned.
The first value must be a boolean (must have the type Bool
).
The second value and third value can be of any type.
The result type is the least common supertype of the second and third value.
1let x = 1 > 2 ? 3 : 42// `x` is `4` and has type `Int`34let y = 1 > 2 ? nil : 35// `y` is `3` and has type `Int?`
The static casting operator as
can be used to statically type cast a value to a type.
If the static type of the value is a subtype of the given type (the "target" type), the operator returns the value as the given type.
The cast is performed statically, i.e. when the program is typechecked. Only the static type of the value is considered, not the runtime type of the value.
This means it is not possible to downcast using this operator.
Consider using the conditional downcasting operator as?
instead.
1// Declare a constant named `integer` which has type `Int`.2//3let integer: Int = 145// Statically cast the value of `integer` to the supertype `Number`.6// The cast succeeds, because the type of the variable `integer`,7// the type `Int`, is a subtype of type `Number`.8// This is an upcast.9//10let number = integer as Number11// `number` is `1` and has type `Number`1213// Declare a constant named `something` which has type `AnyStruct`,14// with an initial value which has type `Int`.15//16let something: AnyStruct = 11718// Statically cast the value of `something` to `Int`.19// This is invalid, the cast fails, because the static type of the value is type `AnyStruct`,20// which is not a subtype of type `Int`.21//22let result = something as Int
The conditional downcasting operator as?
can be used to dynamically type cast a value to a type.
The operator returns an optional.
If the value has a runtime type that is a subtype of the target type
the operator returns the value as the target type,
otherwise the result is nil
.
The cast is performed at runtime, i.e. when the program is executed, not statically, i.e. when the program is checked.
1// Declare a constant named `something` which has type `AnyStruct`,2// with an initial value which has type `Int`.3//4let something: AnyStruct = 156// Conditionally downcast the value of `something` to `Int`.7// The cast succeeds, because the value has type `Int`.8//9let number = something as? Int10// `number` is `1` and has type `Int?`1112// Conditionally downcast the value of `something` to `Bool`.13// The cast fails, because the value has type `Int`,14// and `Bool` is not a subtype of `Int`.15//16let boolean = something as? Bool17// `boolean` is `nil` and has type `Bool?`
Downcasting works for concrete types, but also works e.g. for nested types (e.g. arrays), interfaces, optionals, etc.
1// Declare a constant named `values` which has type `[AnyStruct]`,2// i.e. an array of arbitrarily typed values.3//4let values: [AnyStruct] = [1, true]56let first = values[0] as? Int7// `first` is `1` and has type `Int?`89let second = values[1] as? Bool10// `second` is `true` and has type `Bool?`
The forcedowncasting operator as!
behaves like the
conditional downcasting operator as?
.
However, if the cast succeeds, it returns a value of the given type instead of an optional,
and if the cast fails, it aborts the program instead of returning nil
,
1// Declare a constant named `something` which has type `AnyStruct`,2// with an initial value which has type `Int`.3//4let something: AnyStruct = 156// Forcedowncast the value of `something` to `Int`.7// The cast succeeds, because the value has type `Int`.8//9let number = something as! Int10// `number` is `1` and has type `Int`1112// Forcedowncast the value of `something` to `Bool`.13// The cast fails, because the value has type `Int`,14// and `Bool` is not a subtype of `Int`.15//16let boolean = something as! Bool17// Runtime error
The nilcoalescing operator ??
returns
the value inside an optional if it contains a value,
or returns an alternative value if the optional has no value,
i.e., the optional value is nil
.
If the lefthand side is nonnil, the righthand side is not evaluated.
1// Declare a constant which has an optional integer type2//3let a: Int? = nil45// Declare a constant with a nonoptional integer type,6// which is initialized to `a` if it is nonnil, or 42 otherwise.7//8let b: Int = a ?? 429// `b` is 42, as `a` is nil
The nilcoalescing operator can only be applied to values which have an optional type.
1// Declare a constant with a nonoptional integer type.2//3let a = 145// Invalid: nilcoalescing operator is applied to a value which has a nonoptional type6// (a has the nonoptional type `Int`).7//8let b = a ?? 2
1// Invalid: nilcoalescing operator is applied to a value which has a nonoptional type2// (the integer literal is of type `Int`).3//4let c = 1 ?? 2
The type of the righthand side of the operator (the alternative value) must be a subtype of the type of lefthand side, i.e. the righthand side of the operator must be the nonoptional or optional type matching the type of the lefthand side.
1// Declare a constant with an optional integer type.2//3let a: Int? = nil4let b: Int? = 15let c = a ?? b6// `c` is `1` and has type `Int?`78// Invalid: nilcoalescing operator is applied to a value of type `Int?`,9// but the alternative has type `Bool`.10//11let d = a ?? false
The forceunwrap operator (!
) returns
the value inside an optional if it contains a value,
or panics and aborts the execution if the optional has no value,
i.e., the optional value is nil
.
1// Declare a constant which has an optional integer type2//3let a: Int? = nil45// Declare a constant with a nonoptional integer type,6// which is initialized to `a` if `a` is nonnil.7// If `a` is nil, the program aborts.8//9let b: Int = a!10// The program aborts because `a` is nil.1112// Declare another optional integer constant13let c: Int? = 31415// Declare a nonoptional integer16// which is initialized to `c` if `c` is nonnil.17// If `c` is nil, the program aborts.18let d: Int = c!19// `d` is initialized to 3 because c isn't nil.
The forceunwrap operator can only be applied to values which have an optional type.
1// Declare a constant with a nonoptional integer type.2//3let a = 145// Invalid: forceunwrap operator is applied to a value which has a6// nonoptional type (`a` has the nonoptional type `Int`).7//8let b = a!
1// Invalid: The forceunwrap operator is applied2// to a value which has a nonoptional type3// (the integer literal is of type `Int`).4//5let c = 1!
Operators have the following precedences, highest to lowest:
 Unary precedence:

,!
,<
 Cast precedence:
as
,as?
,as!
 Multiplication precedence:
*
,/
,%
 Addition precedence:
+
,
 Bitwise shift precedence:
<<
,>>
 Bitwise conjunction precedence:
&
 Bitwise exclusive disjunction precedence:
^
 Bitwise disjunction precedence:

 NilCoalescing precedence:
??
 Relational precedence:
<
,<=
,>
,>=
 Equality precedence:
==
,!=
 Logical conjunction precedence:
&&
 Logical disjunction precedence:

 Ternary precedence:
? :
All operators are leftassociative, except for the following operators which are rightassociative:
 Ternary operator
 Nilcoalescing operator
Expressions can be wrapped in parentheses to override precedence conventions,
i.e. an alternate order should be indicated, or when the default order should be emphasized
e.g. to avoid confusion.
For example, (2 + 3) * 4
forces addition to precede multiplication,
and 5 + (6 * 7)
reinforces the default order.