Operators In Swift


The operator is a special symbol or phrase used to check the values or changing the values. The addition operator adds two numbers together. Swift supports most standard c operators and to eliminate common coding errors. Arithmetic operators detect and disallow value overflow and to avoid unexpected results when working with numbers.
Swift allows remainder calculations on floating-point numbers and provide two range operators. 
Unary operator
It operates on a single target. Unary prefix operators appear before the target and unary postfix operators appear after the target.
Binary operator
It operates on two targets and infix and appears between two targets.
Ternary operator
It operates on three targets and Swift has only a ternary operator.

Assignment Operator

The assignment operator initialize or update the value and if the right side of the assignment is a tuple with multiple values its elements decomposed into multiple constants. The assignment operator in swift does not return a value. 
let b = 10
var a = 5
a = b

Arithmetic operator

Swift has four arithmetic operators like addition, subtraction, multiplication, and division. Swift arithmetic operator does not allow value to overflow by default and it supports the string operation in it.
1 + 2
"hello" + "world"

Remainder Operator

The remainder operator is represented as modulo operator and to determine the solution for a % b the % operator calculates the below equation a= (b * multiplier )+ remainder. the multiplier must be the largest number of multiples of b. The same method is used to calculate the remainder for a negative value.
9 % 4

Increment and Decrement operator

Swift provides an increment operator ++ and a decrement operator to increase or decrease the value of a numeric variable by 1. The operator with variables of any integer or floating-point type is used. The ++ and -- symbol is used as a prefix operator or postfix operator. If the operator is before the variable, it increments the variable before returning its value or if the operator is after the variable, it increments the variable after returning its value.
  1. var a = 0      
  2. let b = ++a      
  4. // a and b are equal to 1      
  6. let c = a++      
  8. // a has the value 2 but c  has set to pre increment value of 1  
Unary Minus Operator
The unary minus operator is prepended directly before the value it operates without any white space and the unary plus operator simply return the value it operates on without any change: The unary plus operator does not do anything and it is used to provide symmetry in the code for positive numbers and using unary minus operator for negative numbers.
  1. let three = 3      
  2. let minusThree  = -three      
  3. // minusThree equals -3      
  5. let plusThree = -minusThree      
  6. // plusThree equal 3       
  8. let minusSix = -6      
  9. let alsoMinusSix = +minusSix       
  10. //alsoMinusSix equals -6    
Compound Assignment operator
Swift provides a compound assignment operator that combines assignment = with another operation. The expression a += 2 is shorthand for a = a+2.The addition and assignment are combined into one operator that performs both tasks at the same time and the operator does not return a value.
Comparison Operator
Swift support standard c comparision operators like equal to, not equal to, greater than, Less than, Greater than, Greater than or equal to, less than or equal to and each comparision operators return a Bool value to indicate the statement is true or not. It is mainly used in conditional statement to check conditions.
  1. let name = "world"  
  2. if name == "world" {  
  3.     println("hello,world")  
  4. }  
  5. else {  
  6.     println("\(Name) not match")  
  7. }  
  8. //print hello,world because it matches    
Ternary Conditional Operator
The ternary operator with three parts and it is a shortcut for evaluating one of two expression based on question is true or false. If question is true it return value or it evaluate its value. The ternary operator is shorthand for tha code below,
  1. if question {  
  2.     answer1  
  3. else {  
  4.     answer2  
  5. }  

Logical operator

Logical operators modify or combine the boolean logic values true and false. Swift supports three standard logical operators namely Logical Not, Logical AND, Logical OR.
Logical NOT 
It inverts a Boolean value so true becomes false and false becomes true. The logical NOT operator is a prefix operator and it appears before the value it operates on without whitespace.
  1. let allowedEntry = false      
  2. if !allowedEntry      
  3. {      
  4.    println ("ACCESS DENIED")      
  5. }    
Logical AND 
The logical operator creates logical expressions where both values must be true for overall expression to also be true. The below example consist of two bool values and allows access if both are true,
  1. let enteredDoorCode = true  
  2. let passedRetinaScan = false  
  3. if enteredDoorCode && passedRetinaScan {  
  4.         println(""  
  5.             Welcome ")      
  6.         }  
  7.         else {  
  8.             println("ACCESS DENIED")  
  9.         }  
  10.         //prints "ACCESS DENIED"    
Logical OR
The logical OR operator is an infix made from two adjacent pipe characters. It is used to create logical expressions only one of two values has to be true for overall expression be true. The logical OR operator uses a short circuit evaluation to consider its expressions.