There are many kinds of operators in Swift language. Complete detail along with the examples has been given below.
Assignment Operator (=)
Assignment operator is used to give initial value to a variable or constant. Moreover, assignment operator can be used to assign a new value to a variable.
let oddNumber = 3 var evenNumber = 2 evenNumber = oddNumber+1
If the right hand side of the assignment operator is tuple with multiple values then these values can be assigned to multiple constants or variables at once. For example,
let (selectedColor,selectedNumber) = ("Red",5)
Now selectedColor constant will have value "Red", and selectedNumber constant will have value 5.
Addition Operator (+)
Addition operator is used to add 2 values. For example,
var additionResult = 2+7
So now additionResult variable will have value 9.
There is another purpose of + operator i.e. to concatenate the strings.
let name = "Omer" var greeting = "Hello, " + name
Subtraction Operator (-)
Subtraction operator is used to subtract second value from the first value. For example,
var subtractionResult = 8-3
So now subtractionResult variable will have value 5.
Multiplication Operator (*)
Multiplication operator is to multiply 2 values. For example,
var multiplicationResult = 13*2
So now multiplicationResult variable will have value 26.
Division Operator (/)
Division operator is to divide first value by second value. For example,
var divisionResult = 6/2
So now divisionResult variable will have value 3.
Remainder Operator (%)
For Remainder operator, % symbol is used. As per rule of mathematics, it returns the remainder of 2 values. For example,
var remainderResult = 8%3
So now remainderResult variable will have value 2.
Compound Assignment Operators
Compound Assignment operators first apply the operation for example, addition, subtraction, multiplication, division, and remainder and then assign the result to the variable.
var compoundResult = 2 compoundResult += 3
So now compoundResult will have value 5.
Equal To Operator (==)
Equal To operator is used to compare 2 values/expressions for equality and return true or false basing on the comparison.
var equalToResult = 3==3
So now equalToResult will have value true.
Not Equal To Operator (!=)
Not Equal To operator is used to compare 2 values/expressions for not equal and return true or false basing on the comparison. If two values/expressions are not equal to each other then it would return true, else false.
var notEqualToResult = 5!=2
So now notEqualToResult will have value true.
Greater Than Operator (>)
Greater Than operator is used to compare 2 values/expressions for checking if first value/expression is greater than second value/expression.
var greaterThanResult = 6>6
So now greaterThanResult will have value false.
Less Than Operator (<)
Less Than operator is used to find out if first value/expression is less than second value/expression.
var lessThanResult = 3<5
So now lessThanResult will have value true.
Greater Than Or Equal To Operator (>=)
Greater Than Or Equal To operator is used to compare 2 values/expressions to analyze that if first value/expression is either greater than or equal to second value/expression.
var greaterThanOrEqualToResult = 4>=4
So now greaterThanOrEqualToResult will have value true.
Less Than Or Equal To Operator (<=)
Less Than Or Equal To operator is used to compare 2 values/expressions to check if first value/expression is either less than or equal to second value/expression.
var lessThanOrEqualToResult = 8<=4
So now lessThanOrEqualToResult will have value false.
Nil-Coalescing Operator
The purpose of nil-coalescing operator (x ?? y) to unwrap an optional x if it contains a value, or to return a default value y if x is nil. Note that the expression x is always of an optional type. Moreover, the expression y must match the type that is stored inside x.
Ternary Conditional Operator
Ternary Conditional Operator is kind of if-else statement. It condition is true, it returns first value, otherwise returns second value. For example,
let even = 8 var anyEvenNumber = (even%2)==0?even:4
Closed Range Operator (...)
Closed Range operator which is written with the symbol “...” includes both bounds. For example,
for idx in 1...10 { print("\(idx) times 7 is \(idx * 7)") }
This for loop will run 10 times starting from 1 to 10.
Half Open Range Operator (..<)
Half Open Range operator having symbol “..<” includes first bound but does not include second bound. For example,
for idx in 1..<5 { print("Welcome to omerjaved.com") }
This for loop will run 4 times starting from 1 to 4.
Logical AND Operator (&&)
If both expressions are true then it returns true, otherwise false.
Logical OR Operator (||)
If any of the expression returns true then it returns true.