Welcome to FutureAppLaboratory

v=(*^ワ^*)=v

Learning Swift Part 3 - Basic Operators

| Comments

Just some notes after reading Apple’s official online guide. Most code are from the guide, with some modification by me.

===== Full Notes =====

Basic Operators

  • Terminology
1
2
3
    - Unary
    - Binary
    - Ternary (There is only one in a ? b :c)
  • Assignment Operator
1
2
3
4
    // Not a vaild assignment
    if x = y {

    }
  • Arithmetic Operators
    • Not allow overflow.
    • You can opt in to value overflow behavior by using Swift’s overflow operators (such as a &+ b).
    • The addition operator is also supported for String concatenation.
1
2
3
4
        let dog: Character = "🐶"
        let cow: Character = "🐮"
        let dogCow = dog + cow
        // dogCow is equal to "🐶🐮"
  • Remainder Operator
1
2
3
    9 % 4 // equals 1
    -9 % 4 // equals -1
    -9 % 4 // equals -9 % -4

  • Floating-Point Remainder Calculations
1
    8 % 2.5 // equals 0.5
  • Increment and Decrement Operators
1
2
3
4
5
    var a = 0
    let b = ++a
    // a and b are now both equal to 1
    let c = a++
    // a is now equal to 2, but c has been set to the pre-increment value of 1
  • Unary Minus Operator and Unary Plus Operator
1
2
3
4
5
6
    let three = 3
    let minusThree = -three       // minusThree equals -3
    let plusThree = -minusThree   // plusThree equals 3, or "minus minus three"

    let minusSix = -6
    let alsoMinusSix = +minusSix  // alsoMinusSix equals -6
  • Compound Assignment Operators
1
2
    var a = 1
    a += 2
  • Comparison Operators
1
2
3
4
5
6
    Equal to (a == b)
    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)
  • 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.

1
2
3
4
5
    let defaultColorName = "red"
    var userDefinedColorName: String?   // defaults to nil

    var colorNameToUse = userDefinedColorName ?? defaultColorName
    // userDefinedColorName is nil, so colorNameToUse is set to the default of "red"
  • Range Operators
1
2
3
4
5
6
7
8
    for index in 1...5 {
        println("\(index) times 5 is \(index * 5)")
    }
    // 1 times 5 is 5
    // 2 times 5 is 10
    // 3 times 5 is 15
    // 4 times 5 is 20
    // 5 times 5 is 25
  • Half-Open Range Operator
1
2
3
4
5
6
7
8
9
    let names = ["Anna", "Alex", "Brian", "Jack"]
    let count = names.count
    for i in 0..<count {
        println("Person \(i + 1) is called \(names[i])")
    }
    // Person 1 is called Anna
    // Person 2 is called Alex
    // Person 3 is called Brian
    // Person 4 is called Jack>
  • Logical Operators
1
2
3
4
    Logical NOT (!a)
    Logical AND (a && b)
    Logical OR (a || b)
    Logical NOT (!a)
  • Combining Logical Operators
1
2
3
4
5
6
    if enteredDoorCode && passedRetinaScan || hasDoorKey || knowsOverridePassword {
        println("Welcome!")
    } else {
        println("ACCESS DENIED")
    }
    // prints "Welcome!"
  • Explicit Parentheses
1
2
3
4
5
6
    if (enteredDoorCode && passedRetinaScan) || hasDoorKey || knowsOverridePassword {
        println("Welcome!")
    } else {
        println("ACCESS DENIED")
    }
    // prints "Welcome!"

Comments