Swift Operators

Leela Prasad
4 min readJul 1, 2018

--

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 in the following one.

let total = val1 + val2

&&’ (logical AND)and ‘||’(logical OR) are used to check the conditions by combining other expressions. Example:

if (passedInExams && gotMoreThan90Percentage) || (hasBrain) {

//Welcome to swift programming…

}

  • swift provides all the basic C-language operators, and in addition to those, it provides new ones that didn’t exist in Objective -C as well. For example, the following range operators:

a..<b , a…b as a shortcut to express range values.

Terminology: operators are unary, binary and ternary.

  • Unary Operators: Unary operators operate on a single target, such as ‘+a’ or ‘-a’.

Unary Prefix operators appear immediately before their target like !(b) or !(isAllowed). Unary Postfix operators appear immediately after their target like b! or (isAllowed!).

  • Binary Operators: Binary operators operate on two targets, and are infix, because they appear in-between their targets, like let xVal = (a + b)// ‘+’ and then ‘=’ operate between two operands.
  • Ternary Operators: Ternary operators operate on three targets. swift has only one ternary operator like (condition ? executeThisUponTrue : executeThisUponFalse).

Operators: +, -,*,/, %(Modulo operator, but in swift called as reminder operator which gives reminder in return).

Unary Minus and Unary Plus Operators ->

let myVariableValue = 54

var myNewValue = -myVariableValue // equals -54

Unary Plus operator simply returns the same value without any change

Ex: var myAnotherVal = +myNewValue //Equals -54

Compound Assignment Operators: Swift provides compound assignment operators that combine assignment with another operation like

var aValue = 23

aValue += 5 //Equals 28

  • Comparison Operators

Swift supports all C-language comparison operators. listed below:

> Equal to : (a == b) // returns boolean value

>Not equal to : (a != b)

>Greater than : (a > b)

>Less than : (a <b)

>Greater than or equal to : (a > = b)

>Less than or equal to : (a <= b)

→ Tuples also can be compared, if they have the same type and the same number of values. Tuples are compared from left to right, one value at a time until the comparison finds two tuple values that are not equal.

Example :

  • (3, “Sydney”) > (7, “Atlanta”) // results false because, 3 and 7 are different values, and 3>7 evaluates false, so the final result for the tuple comparison results false.
  • (5, 8) > (5, 5) // results true, because 5 and 5 are the same, so it goes to the next value for comparison, which evaluates 8>5, results true.

** Nil-Coalescing Operator:

The nil coalescing operator (a ?? b)unwraps an optional a if it contains a value, or returns the default value b if a value is nil. The expression of ‘a’ must be optional type, and the type of b must match the a’s type.

Ex: var resultString = (a == b ? “Equal” : “Not Equal”) // if a = 5, b = 5, it results “Equal”

  • Range Operators :

Swift includes several range operators, which are shortcuts for expressing a range of values.

→ Closed Range Operator: The closed range operator (a…b) defines a range that runs from ‘a’ to ‘b’ and includes the values ‘a’ and ‘b’. The value of ‘a’ must not be greater than ‘b’.

Example:

for index in 1…5 {

print(index) // executes 5 times

}

→ Half-Open Range Operator: The half-open range operator (a..<b) defines a range that runs from ‘a’ to ‘b’, but doesn’t include ‘b’. it is called to be half-open because it contains its first value, but not its final value. The value of ‘a’ must not be greater than of ‘b’ value. incase if the ‘a’ and ‘b’ are equal, then the range will be empty.

→ One Sided Ranges : The closed range operator has an alternative form for ranges that continue as far as possible in one direction. For example, if we want to continue from certain index of an Array to the end of the array, we can omit the closed range value. This is called One sided range because, the range is defined with only one side.

Example 1:

let names = [“name1”, “name2”, “name3”, “name4”]

//The following loop prints names from 2nd index till the end of array list.

for name in names[2…] {

print(name)

}

Output: name3, name4

Example 2:

//The following loop prints names from 0th index till the 1st index of array list.

for name in names[…1] {

print(name)

}

Output: name1, name2

Example 3:

//The following loop prints names from 0th index till the 2nd index of array list. It omits the last index, as it is half opened range.

for name in names[..<3] {

print(name)

}

Output: name1, name2, name3

  • Logical Operators : Logical operators modify or combine the boolean logic values true and false. They are

→ Logical NOT (!a) : It inverts the boolean value. It is a prefix operator so appears before the operand.

→ Logical AND (a && b) : Both a and b values must be true, for the overall expression to be true. If the first value(a) is false, it doesn’t check the next one as it does not change the output.

→ Logical OR (a || b) : One of the values need to be true for the over all expression to be true. If the first value (a) is true, it also does not need to check the other value as the output does not get affected by the other value.

--

--