English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Swift Operators

In this article, you will learn about all the knowledge related to different types of operators, their syntax, and how to use them in examples in the Swift programming language.

Operators are special symbols (characters) that operate on operands (variables and values). Some basic operations include assignment, modification, merging, and checking values.

For example,+ is the operator for performing addition.

inSwift variables and constantsIn the article, you learned about variables/Constants. Now, in this article, you will use operators on them.

Type of operator

You can roughly divide operators into two basic categories based on the following content:

  1. Number of operands

  2. Operation of the operator

According to the number of operands operated on by the operator operation, operators can be classified as:

1.unary operator

This operator performs operations on a single operand.

Example1: unary operator

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

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

false
5

2.binary operator

The operator performs operations on two operands.

Example2: binary operator

let result = 20 + 30
print(result)

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

50

3.ternary operator

This operator performs operations on three operands. AccessSwift Ternary Conditional OperatorTo learn more information.

Example3: 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 toOperation of the operator, it can be classified as:

1.assignment operator

Quickly use the assignment operator to assign a value to a property (variable/constant).

Swift assignment operator
OperatorDescription
=Simple assignment operator, assigns the value from the right operand to the left operand
+=Assign after addition, assign the result of adding both operands to the left operand.
-=Assign after subtraction, assign the result of subtracting both operands to the left operand.
*=Assign after multiplication, assign the result of multiplying both operands to the left operand.
/=Assign after division, assign the result of dividing both operands to the left operand.
%=Assign after taking the modulus, assign the modulus of both operands to the left operand.
<<=Left shift and then assign
>>=Right shift and then assign
&=Bitwise AND operation followed by assignment
^=Bitwise XOR operation followed by assignment
|=Bitwise OR operation followed by assignment

Example4Simple Assignment Operator

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

When running the program, the output is:

10
iOS

The above examples store the integer value10Assigned to the constant age. Therefore, the statement print(Age) outputs10.

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

Example5Compound Assignment Operators

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

When running the program, the output is:

8

Expression x-=2Use compound assignment operators(-=),is x=x-2Abbreviation. This operator is a compound assignment operator because it performs both subtraction and assignment tasks at the same time.

You can find more information in this articleSwift BitwiseExamples of bitwise operators can be found on the operator.

2.Arithmetic Operators

These operators are used to perform mathematical operations, including multiplication, division, addition, and subtraction, etc. This operator is a binary operator that takes two operands.

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

Example6Simple 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") //Operators can also be used to concatenate strings

When running the program, the output is:

30
-10
10
2
1
I love Swift

Example7Arithmetic Operators

You can use assignment operators to store the result in a variable or constant, as shown below:

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

When running the program, the output is:

2
1

3.Comparison Operators

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

Swift Comparison Operators
OperatorDescriptionExample
==Equal5 == 3Evaluates to false
!=Not equal5!= 3Evaluates to true
>Greater
5> 3 Evaluates to true
<Less than5 <3 Evaluates to false
>=Greater than or equal to5>= 5Evaluates to true
<=Less than or equal to4 <= 5Evaluates to true

Example8Comparison operators

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

When running the program, the output is:

true
false

Example9Comparison operators: greater than and less than

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

When running the program, the output is:

false
true
true
false

4.Logical Operators

These operators are used with boolean (logical) values and return a boolean value. They are mainly used to specify the flow of the program through if else, while, or other control statements.

Swift Logical Operators
OperatorDescriptionExample
||Logical OR; true if any boolean expression is truefalse || true evaluates to true
&&Logical AND; true if all boolean expressions are truefalse && true evaluates to false

Example10: Logical Operators

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

When running the program, the output is:

true
false
true

This article introduces some basic operators in Swift. However, there are few advanced operators in Swift, such asRange Operator,Null Coalescing Operatorwhich you will learn in the next tutorial.

Next, you will learnSwift Operator Precedence and AssociationIn short, this is the execution order of these operations in the expression.