Swift Operators

In this article, you'll learn everything about different types of operators in Swift programming language, their syntax and how to use them with examples.

Operators are special symbols (characters) that carry out operations on operands (variables and values). Some basic operations includes assigning, changing, combining and checking values.

For example, + is an operator that performs addition.

In Swift variables & constants article, you learned about variables/constants. Now, in this article you will the use of operators on them.


Types of operators

You can categorize operators broadly in two basic categories based on the:

  1. Number of of operands
  2. Operation of an operator

According to the number of operands an operator operates on, operators can be categorized as:

1. Unary operator

This operators operate on a single operand.

Example 1: Unary Operator

print(!true)
var a = -5
print(-a)

When you run the above program, the output will be:

false
5

2. Binary operator

This operator operates on a two operands.

Example 2: Binary Operator

let result = 20 + 30
print(result)

When you run the above program, the output will be:

50

3. Ternary Operators

This operator operates on three operands. Visit Swift Ternary Conditional Operator to learn more about it.

Example 3: Ternary Operator

let result = (5 > 10) ? "Value larger" : "Value Smaller"
print(result)

When you run the above program, the output will be:

Value Smaller

According to the operation a operator does, it can be categorized as:

1. Assignment operators

Assignment operator are used in swift to assign values to a property (variable/constant).

Swift Assignment Operators
Operator Meaning
= Simple assignment operator, Assigns values from right side operands to left side operand
+= Add AND assignment operator, It adds right operand to the left operand and assigns the result to left operand
-= Subtract AND assignment operator, It subtracts right operand from the left operand and assigns the result to left operand
*= Multiply AND assignment operator, It multiplies right operand with the left operand and assigns the result to left operand
/= Divide AND assignment operator, It divides left operand with the right operand and assigns the result to left operand
%= Modulus AND assignment operator, It takes modulus using two operands and assigns the result to left operand
<<= Left shift AND assignment operator
>>= Right shift AND assignment operator
&= Bitwise AND assignment operator
^= bitwise exclusive OR and assignment operator
|= bitwise inclusive OR and assignment operator

Example 4: Normal Assignment Operator

let age = 10
let platform = "iOS"
print(age)
print(platform)

When you run the program, the output will be:

10
iOS

The above example assigns the integer value 10 to the constant age. So the statement print(age) outputs 10 in the console.

Likewise, the statement let platform = "iOS" assigns the string literal "iOS" to the constant platform. Therefore, the statement print(platform) outputs iOS in the console.

Example 5: Compound Assignment Operator

var x = 10
x -= 2
print(x)

When you run the program, the output will be:

8

The expression x -= 2 uses a compound assignment operator (-=) and is a shorthand for x = x - 2. The operator is a compound assignment operator because the operator performs both task subtraction and assignment at the same time.

You can find examples on bitwise operators on this article Swift Bitwise operators.


2. Arithmetic operators

These operators are used to perform mathematical operations that includes multiplication, division, addition and subtraction etc. This operators fall into category of binary operator that takes two operands.

Swift Arithmetic Operators
Operator Meaning
+ Addition (also used for string concatenation)
- Subtraction Operator
* Multiplication Operator
/ Division Operator
% Remainder Operator

Example 6: Simple Arithmetic Operations

print(10 + 20)
print(10 - 20)
print(2 * 5)
print(5 / 2 ) //division operator
print(5 % 2 ) //remainder operator
print("I love " + "Swift") //operator can also be used to concatenate string

When you run the program, the output will be:

30
-10
10
2
1
I love Swift

Example 7: Arithmetic Operator

You may use the result to store into a variable or constant using assignment operators as

let x = 10 / 5
let y = 3 % 2
print(x)
print(y)

When you run the program, the output will be:

2
1

3. Comparison Operators

These operators allow you to compare two values. Each of the comparison operators returns a Bool value to indicate whether or not the statement is true. Swift supports following types of comparison operators:

Swift Comparison Operators
Operator Meaning Example
== equal to 5 == 3 is evaluated to false
!= not equal to 5 != 3 is evaluated to true
> greater than 5 > 3 is evaluated to true
< less than 5 < 3 is evaluated to false
>= greater than or equal to 5 >= 5 is evaluated to true
<= less than or equal to 4 <= 5 is evaluated to true

Example 8: Comparison Operator

let msg = "Hello"
print(msg == "Hello")
print(msg != "Hello")

When you run the program, the output will be:

true
false

Example 9: Greater than and less than comparison operators

print(10 > 20)
print(10 < 20)
print(5 >= 5)
print(5 <= 4)

When you run the program, the output will be:

false
true
true
false

4. Logical Operators

These operators are used with Boolean (logical) values and returns a Boolean value. It is mainly used to control program flow with if else, while, or some other control statement.

Swift Logical Operators
Operator Meaning Example
|| Logical-OR; true if either of the boolean expression is true false || true is evaluated to true
&& Logical-AND; true if all boolean expressions are true false && true is evaluated to false

Example 10: Logical Operator

print(true && true)
print(true && false)
print(false || true)

When you run the program, the output will be:

true
false
true

This article explains some of the basic operators in Swift. However, there are few more advanced operators like Range Operators, Nil-Coalescing Operator in Swift which you'll learn in the coming tutorials.

Next, you'll learn about precedence and associativity of Swift operators. Simply said, it is the order of execution of these operations in an expression.