Basic Operators
An operator is a special symbol or phrase that you use to check, change, or combine values. For example, the addition operator + adds two numbers (as in let i = 1 + 2).
Assignment Operator
The assignment operator a = b initializes or updates the value of a with the value of b:
let b = 10
var a = 5
a = b
// a is now equal to 10
Arithmetic Operators
Swift supports the four standard arithmetic operators for all number types:
- Addition (
+) - Subtraction (
-) - Multiplication (
*) - Division (
/)
1 + 2 // equals 3
5 - 3 // equals 2
2 * 3 // equals 6
10.0 / 2.5 // equals 4.0
Compound Assignment Operators
Like C, Swift provides compound assignment operators that combine assignment (=) with another operation. One example is the addition assignment operator (+=):
var a = 1
a += 2
// a is now equal to 3