Operators: special symbols that work with one to three operands.

Syntax:1 + 2 == 3

let c = a && b

Operators can be classified as unary, binary, or ternary depending on how many operands they take as input. A unary operator takes one operand, and it can be defined as prefix or postfix. Binary operands are declared infix because they always occur between the two operands. There is only one ternary operator in Swift, the if(x)-then(y)-else(z) shorthand conditional operator of (x ? y : z).

The Arithmetic Operators work with all of the numeric types. Swift also defines the addition operator for strings, to support concatenation. You can define how the operators should work with your custom classes as well.

// Arithmetic Operators

2 + 3 // 5 - Addition

9 - 1 // 8 - Subtraction

4 * 5 // 20 - Multiplication

6 / 3 // 2 - Division

8 % 5 // 3 - Remainder

The Assignment Operator sets the value of the left operand with the value of the right operand. This even works for Tuples. Unlike Objective-C, however, this operator does NOT return a value. This is a safety feature to catch cases where the "==" operator is needed. There are also a set of compound assignment operators, which only work with variables because the left operand is being modified from an earlier value.

// Assignment Operator

let x = 2

var y = x + 5 // No return value, but y is now 7

// assigning multiple values at once via a tuple

let (a,b,c) = (1,2,3)

// Compound Assignment Operators

var x = 5

x *= 2 // Multiple & assign, x=10

x /= 2 // Divide & assign, x=5 again

x %= 2 // Remainder & assign, x=1

x += 7 // Add & assign, x=8

x -= 3 // Subtract & assign, x=5

x <<= 2 // Left bit shift & assign, x=20

x >>= 2 // Right bit shift & assign, x=5

x &= 3 // Bitwise AND & assign, x=1

x ^= 3 // Bitwise XOR & assign, x=2

x |= 6 // Bitwise OR & assign, x=6

By default, Swift will not let you assign a numeric value that would overflow the type the constant or variable was defined as. This behavior can be overridden by using the Overflow Operators.

var tooFull = UInt8.max // set to 255

tooFull += 1 // COMPILER ERROR, OVERFLOW

tooFull &+ 1 // Handled, tooFull is now 0

// These other ones work the same way

tooFull &- 1 // Overflow subtraction

tooFull &* 50000 // Overflow multiplication

tooFull &/ 0 // Overflow division

tooFull &% 5 // Overflow remainder

There are both prefix and postfix versions of the Increment and Decrement Operators.

// Increment Operator

var a = 1

let b = ++a

let c = a++

In the above example, "a" starts at "1". In the second line, the value of "a" is incremented BEFORE the value is returned, so "b" is set to "2". In the third line, the value of "a" is incremented AFTER the value is returned, so "c" is also set to "2", but "a" is now "3"

// Decrement Operator

var x = 5

let y = --x

let z = x--

The mechanics here are the same as with addition. Thus, the end result is x=3, y=4, and z=4. The prefix version of these operators is generally recommended since it provides the intuitive result of returning the modified value.

Comparison Operators return a Bool value, so you can execute conditional statements based on the result. Swift uses the same comparison symbols as C and many other languages.

// Comparison Operators

5 == 6 // Equal to, returns false

5 != 6 // Not equal to, returns true

5 > 6 // Greater than, returns false

5 < 6 // Less than, returns true

5 >= 6 // Greater than or equal to, returns false

5 <= 6 // Less than or equal to, returns true

// Identity Comparisons to check if two object references are the same

var myVar = MyClass()

var otherVar = myVar

var someOtherVar = MyClass()

myVar === otherVar // returns true

myVar === someOtherVar // returns false

The Nil Coalescing Operator is new in Swift. It unwraps an optional if it has a value, otherwise it returns a default value. Short-circuit evaluation ensures that the optional value will only be evaluated if the optional is nil.

let defaultSetting = "Automatic"

var userInput: String?

var result = userInput ?? defaultSetting

// Since userInput defaults to nil, result is "Automatic"

Swift supports two Range Operators: Closed, and Half-Open. A Closed range (a...b) includes all values starting with "a", up to and including "b". A Half-Open range (a..<b) includes all values starting with "a", but NOT including "b". In both cases, "a" must always be less than "b".

// Closed Range

for students in 200...250 {

println("Student \(students) present and accounted for")

}

// Half-Open Range - best for zero-based arrays

let fruit = ["Apple", "Banana", "Cherry", "Lime"]

for i in 0..<fruit.count {

println("Add to shopping list: \(fruit[i])")

}

Swift supports three Logical Operators: NOT, AND, OR. These work with Boolean logic values "true" and "false". The NOT operator (!) flips the value of the operand, from true to false, or from false to true. The AND operator (&&) returns true only if both operands are true. The OR operator (||) returns true if either operand is true.

// Logical NOT

let positive = true

if !positive {

println("Then you must be negative")

}

// Logical AND

let negative = !positive

if positive && negative {

println("How is this possible??")

}

// Logical OR

if positive || negative {

println("Figured it was one or the other.")

}