DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Ajithmadhan
Ajithmadhan

Posted on • Updated on

Swift Operators

An operator is a special symbol or phrase that you use to check, change, or combine values.

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
Enter fullscreen mode Exit fullscreen mode

Arithmetic Operators

Swift supports the four standard arithmetic operators for all number types:

  • Addition (+)
  • Subtractions (-)
  • Multiplication (*)
  • Division (/)

Remainder (%) operator works out like how many multiples of b will fit inside a and it returns the value thats left over (remainder)

unary operators

  • unary plus (+) operator
  • unary minus (-) operator

Compound Assignment operator (+=)

Like other programming languages compound assignment operator combines the assignment operator with any other arithmetic operators.

var a = 5
a += 10 //a = a + 10
print(a) // 15
Enter fullscreen mode Exit fullscreen mode

Comparison operators

Swift supports the following comparison operators and maximum of cases the comparison operators return a boolean value.

  • Equal to (==)
  • Not equal to (!=)
  • greater than (>)
  • lesser than (<)
  • greater than or equal to (>=)
  • lesser than or equal to (<=)

Ternary operator (?:)

The ternary conditional operator is a special operator with three parts, which takes the form question ? answer1 : answer2.

The ternary operator is the short form of the below code .

let a = 10, b = 20
if(a>b) {
print("A is greater")
}else {
print("B is greater")
}

//prints B is greater 

a > b ? print("A is greater") : print("B is greater")

//prints B is greater 
Enter fullscreen mode Exit fullscreen mode

Nil-coalescing operator (??)

The nil-coalescing operator (a ?? b) unwraps an optional a if it contains a value, or returns a default value b if a is nil.

let a:Int? = nil
let b:Int = a ?? 20
print(b) //prints 20

//this operator is shorthand for

a != nil ? a! : b 

if a != nil {
    print(a!)
}else {
    a = 20
}
print(a!)

Enter fullscreen mode Exit fullscreen mode

Note : If the value of a is not nil then b is not evaluated this is known as short-circuit evaluation

Range operator

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

closed range (a...b)

It defines a range that runs from a to b and it includes the a and b

  • The value of a should not be greater than b
for index in 3...6 {
    print(index)
}
//3 4 5 6
Enter fullscreen mode Exit fullscreen mode

Half-open Range operator (a..<b)

It defines a range that runs from a to b but it does not includes b. It's said to be half-open because it contains the first value but not the final value.

for index in 3..<6 {
    print(index)
}
//3 4 5 
Enter fullscreen mode Exit fullscreen mode

one-sided range (a...),(...b),(..<b)

The closed range operator has an alternative form for ranges that continues from a range that continues as far as possible in one direction.

Logical operators

Logical operators modify or combine the Boolean logic values true and false. Swift supports the three standard logical operators found in C-based languages:

  • Logical not (!a) Returns the reverse of the value
  • Logical Or (a || b) Returns true if any of two values are true
  • Logical And (a && b) Returns true if only both values are true

Top comments (0)

Super Useful CSS Resources

>> Check out this classic DEV post <<