Basic Operators

Terminology

  • Unary operator (such as -a)
  • Binary operator(such as 2 + 3)
  • Ternary operator(such as a ? b : c)

The values that operators affect are operands. Prefix, infix, suffix

Assignment Operator

// Assignmentlet b = 10var a = 5a = b// tuple assignmentlet (x, y) = (1, 2)// not allowed, it is to prevent miss use when the equal operator(==) is actually intended.if x = y {    // This isn't valid, because x = y doesn't return a value.}

Arithmetic Operators

Addition+
Subtraction-
Multiplication*
Division/
1 + 15 - 32 * 310.0 / 2.5"hello, " + "world"   // Addition operator is also supported for String concatenation 

Remainder Operator

b = (a x some multiplier) + remainder

// 9 = 4 + 4 + 19 / 4   // it equals to 29 % 4   // it equals to 1// Calculate method// a % b // b = (a x some multiplier) + remainder-9 % 4  // it equals to -1// -9 = (4 x -2) + -1// The sign of 'b' is ignored.// a % b and a % -b always give the same answer.

Compound Assignment Operators

a += 2   // it equals a = a + 2b -= 3c *= 2d /= 2// The compound assignment operators don't return a value. // let b = a += 2  is illegal

Comparison Operator

Equal toa == b
Not equal toa != b
Greater thana > b
Less thana < b
Greater than or equal toa >= b
Less than or equal toa <= b
// Compare the tuples// from left to right(1, "zebra") < (2, "apple")  // true(3, "apple") < (3, "bird")   // true(4, "dog") == (4, "dog")     // true// The element in the tuple should be comparable, so that the tuple can be comparable(String, Int) < (String, Int)  // It's ok(String, Boolean) < (String, Int) // Error, Boolean can't be compared

Ternary Operator

var a = 3var b = 4c = a > b ? 2 : 1// The code equals tovar a = 3var b = 4if a > b {    c = 2} else {    c = 1}

Nil-Coalescing Operator

Only optional type can use nil-coalescing operator

// unwrap an optional 'a' if it contains a value, or return a default value 'b' if 'a' is nil.a ?? b// The code equals toa != nil ? a! : b// Usagelet defaultColorName = "red"var userDefinedColorName : String?  // It's assign with nil in default.var colorNameToUse = userDefinedColorName ?? defaultColorName// it's reduserDefinedColorName = "Green"colorNameToUse = userDefinedColorName ?? defaultColorName// it's green

Range Operator

Closed Range Operator

// From 1 to 5, include 1 and 5for i in 1...5 {    }

Half-Open Range Operator

// From 1 to 5, include 1 but not 5for i in 1..<5 {    }// it's useful in array traversal

One-Side Range

for name in names[2...] {    // from 2 to the end, include 2}for name in names[...2] {    // from 0 to 2, include 2}for name in names[..<2] {    // from 0 to 2, not include 2}// One-side operator can be used in other contexts, not just in subscripts.let range = ...5range.contains(7)range.contains(5)range.cantains(-3)

Logical Operator

Logical NOT!a
Logical ANDa && b
Logical ORab

Combining Logical Operators

The compound expression are left-associative.

if enteredDoorCode && passedRetinaScan || hasDoorKey || knowsOverridePassword {    print("Welcome!")} else {    print("Access Denied")}

Explicit Parentheses

Add a pair of parentheses to make it easier to read.s

if (enteredDoorCode && passedRetinaScan) || hasDoorKey || knowsOverridePassword {    print("Welcome!")} else {    print("Access Denied")}