Swiftly

Swift 5.1 references for busy coders

Math (+, -, *, /, %)

Addition (+)

print(5 + 10) // 15
print(100 + 25) // 125
// Strings can be added too:
print("foo" + "bar") // "foobar"

Subtraction (-)

print(5 - 10) // -5
print(100 - 25) // 75

Multiplication (*)

print(5 * 10) // 50
print(100 * 25) // 2500

Division (/)

print(5 / 10) // -5
print(100 / 25) // 4

Remainder (%)

The remainder operator can be used to find the modulus: the remainder after division of one number by another.

print(9 % 2) // 1
print(100 % 10) // 0
print(100 % 95) // 5

Unary operators

A prefixed - can be used to toggle the sign of a value. A prefixed + does nothing, but can be used for code symmetry. No space is allowed between the operator and the value.

Unary minus (-)

let a = 5
let b = -a
print(b) // -5

Unary plus (+)

let a = 5
let b = +a
print(b) // 5