Swift


Types

String

String

"This is a string!"

Character

Character

"a"

Int

Integer

64

Float

Float (number with decimal point)

64.0

Double

64 bit float

64.0

Bool

Boolean value (either true or false)

true

Array

Array

Set

Set

Dictionary

Dictionary

Arithmetic operators

+

Addition

Unary positive, makes number positive

-

Subtraction

Unary negative, makes number negative

*

Multiplication

/

Division

%

Modulo (remainder of division)

**

Indice

Assignment operators

=

Equals

+=

a+=b is a=a+b

-=

a-=b is a=a-b

*=

a*=b is a=a*b

/=

a/=b is a=a/b

%=

a%=b is a=a%b

**=

a**=b is a=a**b

&=

a&=b is a=a&b

|=

a|=b is a=a|b

^=

a^=b is a=a^b

>>=

a>>>=b is a=a>>>b

<<=

a><<=b is a=a><<b

Comparison operators

==

Is equal to

===

Has equal value and type to

!=

Is not equal to

!==

Doesn't have equal value or type to

>

Greater than

<

Less than

>=

Greater than or equal to

<=

Less than or equal to

?:

Ternary operator; executes one of two possibilities based on boolean value

isTrue ? "Yes" : "No"

Bitwise operators

&

Bitwise AND

|

Bitwise OR

^

Bitwise XOR

<<

Arithmetic shift left (shifts all the bits one space to the left, essentially multiplying it by 2)

>>

Logical shift right (shifts all the bits one space to the right, essentially dividing it by 2)

~

Bitwise NOT

Logical operators

&&

AND operator

||

OR operator

!

NOT operator

Type operators

typeof

Returns the type of a variable

instanceof

Returns true if an object is an instance of an object type

Miscellaneous operators

.

Applies method to object
Applies a state to an enum type

Denotes class of an object

Denotes instance of an object

[]

Defines an array
Initialises an array
References an array's index

var array:[Int] = []

var array = [1,2,3]

arraylist[0]

,

Used to separate parameters
Used to include mutiple case options

()

Denotes parameters of a function

Denotes a function

{}

Defines a block

:

Used for switch cases

Used for stating a variable's type

Used for stating the value of a variable in parameters

f(arg: value)

Used for denoting superclass

->

Used for declaring a functions return type

""

Declares a string

''

Declares a string

+

Concatenates strings

'These are '+'two strings!'

_

Hides argument label when calling a function

func add(_arg1:Int, _arg2:Int) {

;

Optionally declares end of a line of code

...

Indicates variadic number of arguments and treats them as an array

\()

Used for variable inclusion in a string

?

Declares optional variable

!

Optional variable default value returner

Delcares non-optional variable

Key word

class

Declares a class (class name must have a capital letter at start)

class Class {
 code
}

Declares a class function

struct

Declares a struct, like a class but no inheritance (class name must have a capital letter at start)

struct Structure {
 code
}

mutating

const

Used to declare constant that can't be redeclared throughout entire code

var

Used to declare a variable

let

Used to declare an object that can't be redeclared within current block

extension

Declares an extension; which can introduce new functionality forf a type

super

References the class' superclass

true

Represents a true boolean value

true

false

Represents a false boolean value

false

implements

Used for including an interface in another interface or in a class, creating polymorphism

enum

Declares an enum; a datatype that denotes a "state" of something

enum enum {
 code
}

interface

Used for declaring an interface; like a class but has no variables and methods can be empty

abstract

Used for declaring an abstract class or method, which is a class or method that is only accessible through its subclasses and is defined in its subclass

switch

Displays different outcomes based on the value within the expression

switch expression {
case x:
  code
case y:
  code
}

Double

Delcares doubles (also known as floats, or numbers with decimal place)

Bool

Declares booleans

Int

Declares integers

String

Declares strings

static

Declares static methods (that do not depend on the state of the class)

throw

Throws an error object

throws

Used for denoting methods that throw specific errors

Character

Declares characters

Void

Declares a method with no return value

self

Used for referring to the original constructor of a class on which a new object is being created

init

Constructor of a class (method to make objects of a class)

init() {  code }

deinit

Deilitialisor function of a class (method to deinitialise objects of a class)

deinit {  code }

deinit

Deilitialisor function of a class (method to deinitialise objects of a class)

private

Used for declaring private classes or methods, that cannot be interacted with by other classes

public

Used for declaring public classes or methods, that can be interacted with by other classes

if

Executes code on true condition

if condition {
 code
}

while

Loops code on true condition

while condition {
 //code to loop
}

repeat {
 //code to loop
} while condition

for

Loops code for a set amount iterations or for every object in list

for item in array {
 code
}

try

Attempts to execute code and ignores on error

try {
 code
}

try (declaration) {
 code;
}

finally

Executes code at end of try statement

finally {
 code;
}

do

catch

Executes code on failed try statement

catch (Exception exception)
 code

else

Executes when an if statement is false or if a try statement has no error

else {
 code
}

case

Used for declaring case statement in a switch statement

Used for delcaring states in an enum

import

Imports classes or modules

import package

return

Used to give a final value at the end of a function or method

break

Used to tell compiler to end the loop

default

Default case in a switch statement

final

Used to denote a class with no subclasses or a function that can't be overwritten in a subclass

subscript

A special function within a struct that allows instances of a struct to be treated like an array where the index is a parameter and a value is returned

else {
 code
}

func

Declares a function

func function(arglabel:arg) {
 code
}

func function(arglabel:arg) -> type {
 code
}

mutating func

Declares a mutating function (function that changes state of a class instance)

func function(arglabel:arg) {
 code
}

func function(arglabel:arg) -> type {
 code
}

in

Declares a lambda expression

{  (arglabel:arg) -> type in
  code
 }

Default functions

stride()

stride(string);

prompt()

prompt(string);

Methods

toString

toFixed

charAt

split

concat

toLowerCase

toUpperCase

toFixed

toFixed

Commenting

//

Used to tell compiler the rest of this line is not to be compiled

/*

Opens a multi line comment

*/

Closes a multi line comment

Obfuscation

Minification

Packing

Encoding

Base64

Encoding that uses alpha-numeric encoding as well as + and /. It uses = as padding to ensure every string has a length that is a multiple of 4

Hex

Encoding that contains numbers 0 to 9 as well as letters A, B, C, D, E, and F, that maps ASCII characters to their hex representation

Rot13

Encoding that translates each letter to the letter 13 letters in front, a form of caeser cypher