Show Menu
Cheatography

 Swift Programming Language Cheat Sheet  Cheat Sheet (DRAFT) by

This is a cheat sheet to the topics that I use the most.

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Arithmetic Operators

Operator Purpose
+ Addition
- Subtra­ction
* Multip­lic­ation
/ Division
% Remainder

OPERATORS

Arithmetic Operators

+ Addition
- Subtra­ction
* Multip­lic­ation
/ Division
% Remainder
Note: Unlike the remainder operator in C and Object­ive-C, Swift’s remainder operator can also operate on floati­ng-­point numbers (e.g. 8 % 2.5 // equals 0.5)

Compar­ative Operators

== Equal to
=== Identical to
!= Not equal to
!== Not identical to
~= Pattern match
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to


Assignment Operators

= Assign
+= Addition
-= Subtra­ction
*= Multip­lic­ation
/= Division
%= Remainder
&= Bitwise AND
|= Bitwise Inclusive OR
^= Exclusive OR
<<= Shift Left
>>= Shift Right
&&= Logical AND
||= Logical OR


Increment and Decrement Operators

++ Addition
-- Subtra­ction
If the operator is written before the variable, it increments the variable before returning its value.
If the operator is written after the variable, it increments the variable after returning its value.


Logical Operators

! NOT
&& Logical AND
|| Logical OR


Range Operators

..< Half-open range
... Closed range


Bitwise Operators

& Bitwise AND
| Bitwise Inclusive OR
^ Exclusive OR
~ Unary complement (bit inversion)
<< Shift Left
>> Shift Right

Overflow and Underflow Operators
Typically, assigning or increm­enting an integer, float, or double past it's range would result in a runtime error. However, if you'd instead prefer to safely truncate the number of available bits, you can opt-in to have the variable overflow or underflow using the following operators:
Operator Purpose
&+ Addition
&- Subtra­ction
&* Multip­lic­ation
&/ Division
&% Remainder
Example for unsigned integers (works similarly for signed):

var willOv­erflow = UInt8.max
// willOv­erflow equals 255, which is the largest value a UInt8 can hold
willOv­erflow = willOv­erflow &+ 1
// willOv­erflow is now equal to 0

var willUn­derflow = UInt8.min
// willUn­derflow equals 0, which is the smallest value a UInt8 can hold
willUn­derflow = willUn­derflow &- 1
// willUn­derflow is now equal to 255
Another example to show how you can prevent dividing by zero from resulting in infinity:

let x = 1
let y = x &/ 0
// y is equal to 0

Other Operators

?? Nil coalescing
?: Ternary condit­ional
! Force unwrap object value
? Safely unwrap object value
 

Comper­ative Operators

Operator Purpose
== Equal to
=== Identical to
!= Not equal to
!== Not identical to
~= Pattern match
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
 

Assignment Operators

Operator Purpose
= Assign
+= Addition
-= Subtra­ction
*= Multip­lic­ation
/= Division
%= Remainder
&= Bitwise AND
|= Bitwise Inclusive OR
^= Exclusive OR
<<= Shift Left
>>= Shift Right
&&= Logical AND
||= Logical OR

Bitwise Operators

Operator Purpose
& Bitwise AND
| Bitwise Inclusive OR
^ Exclusive OR
~ Unary complement (bit inversion)
<< Shift Left
>> Shift Right
 

Increment and Decrement Operators

Operator Purpose
++ Addition
-- Subtra­ction
-If the operator is written before the variable, it increments the variable before returning its value.

-If the operator is written after the variable, it increments the variable after returning its value.

Logical Operators

Operator Purpose
! NOT
&& Logical AND
|| Logical OR

Range Operators

Operator Purpose
..< Half-open range
... Closed range