美文网首页iOS劝退指南
Swift4.2_操作运算符

Swift4.2_操作运算符

作者: 鼬殿 | 来源:发表于2018-11-22 20:38 被阅读12次

    官网链接

    • 赋值运算符 (Assignment Operator)
    let b = 10
    var a = 5
    a = b
    // a is now equal to 10
    
    let (x, y) = (1, 2)
    // x is equal to 1, and y is equal to 2
    
    if x = y {
        // This is not valid, because x = y does not return a value.
    }
    
    • 算术运算符 (Arithmetic Operators)

    Addition (+)
    Subtraction (-)
    Multiplication (*)
    Division (/)

    1 + 2       // equals 3
    5 - 3       // equals 2
    2 * 3       // equals 6
    10.0 / 2.5  // equals 4.0
    
    "hello, " + "world"  // equals "hello, world"
    
    • 取余运算符 (Remainder Operator)
    9 % 4    // equals 1(等同于下面的表达式)
    9 = (4 x 2) + 1
    
    -9 % 4   // equals -1
    -9 = (4 x -2) + -1
    
    • 一元减运算符 (Unary Minus Operator)
    let three = 3
    let minusThree = -three       // minusThree equals -3
    let plusThree = -minusThree   // plusThree equals 3, or "minus minus three"
    
    • 一元加运算符 (Unary Plus Operator)

    一元+运算符不对操作的变量做任何的修改,返回变量本身

    let minusSix = -6
    let alsoMinusSix = +minusSix  // alsoMinusSix equals -6
    
    • 复合赋值运算符 (Compound Assignment Operators)
    var a = 1
    a += 2
    // a is now equal to 3
    

    NOTE:
    复合赋值运算符不返回值。 例如,你不能写
    let b = a += 2

    • 比较运算符 (Comparison Operators)

    Swift支持所有标准C比较运算符:

    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)
    

    Swift还提供了两个标识运算符(===!==),用于测试两个对象引用是否都引用同一个对象实例。

    1 == 1   // true because 1 is equal to 1
    2 != 1   // true because 2 is not equal to 1
    2 > 1    // true because 2 is greater than 1
    1 < 2    // true because 1 is less than 2
    1 >= 1   // true because 1 is greater than or equal to 1
    2 <= 1   // false because 2 is not less than or equal to 1
    

    比较运算符通常用于条件语句,例如if语句:

    let name = "world"
    if name == "world" {
        print("hello, world")
    } else {
        print("I'm sorry \(name), but I don't recognize you")
    }
    // Prints "hello, world", because name is indeed equal to "world".
    

    两个元组如果具有相同的类型和相同数量的值,也可以比较,从左到右,一次比较一个值

    (1, "zebra") < (2, "apple")   // true because 1 is less than 2; "zebra" and "apple" are not compared
    (3, "apple") < (3, "bird")    // true because 3 is equal to 3, and "apple" is less than "bird"
    (4, "dog") == (4, "dog")      // true because 4 is equal to 4, and "dog" is equal to "dog"
    
    ("blue", -1) < ("purple", 1)        // OK, evaluates to true
    ("blue", false) < ("purple", true)  // Error because < can't compare Boolean values
    

    NOTE:
    Swift标准库包含元组的元组比较运算符,少于7个元素。 要将元组与七个或更多元素进行比较,您必须自己实现比较运算符。

    • 三元运算符 (Ternary Conditional Operator)
    if question {
        answer1
    } else {
        answer2
    }
    
    let contentHeight = 40
    let hasHeader = true
    let rowHeight = contentHeight + (hasHeader ? 50 : 20)
    // rowHeight is equal to 90
    上面的示例是下面代码的简写:
    
    let contentHeight = 40
    let hasHeader = true
    let rowHeight: Int
    if hasHeader {
        rowHeight = contentHeight + 50
    } else {
        rowHeight = contentHeight + 20
    }
    // rowHeight is equal to 90
    
    • 空合运算符 (Nil-Coalescing Operator)

    nil-coalescing运算符(a ?? b)展开,如果a有值就返回a,如果anil就返回b。 表达式a始终是可选类型。 表达式b必须与存储在a中的类型匹配。

    nil-coalescing运算符是下面代码的简写:

    a != nil ? a! : b
    

    NOTE:
    如果a的值不为nil,则不评估b的值。 这被称为短路求值。

    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"
    
    userDefinedColorName = "green"
    colorNameToUse = userDefinedColorName ?? defaultColorName
    // userDefinedColorName is not nil, so colorNameToUse is set to "green"
    
    • 区间运算符 (Range Operators)

    1.闭区间运算符(Closed Range Operator)

    for index in 1...5 {
        print("\(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
    

    2.半区间运算符(Half-Open Range Operator)

    let names = ["Anna", "Alex", "Brian", "Jack"]
    let count = names.count
    for i in 0..<count {
        print("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
    

    3.单区间运算符 (One-Sided Ranges)

    for name in names[2...] {
        print(name)
    }
    // Brian
    // Jack
    
    for name in names[...2] {
        print(name)
    }
    // Anna
    // Alex
    // Brian
    
    for name in names[..<2] {
        print(name)
    }
    // Anna
    // Alex
    
    let range = ...5
    range.contains(7)   // false
    range.contains(4)   // true
    range.contains(-1)  // true
    
    • 逻辑运算符 (Logical Operators)
    Logical NOT (!a)
    Logical AND (a && b)
    Logical OR (a || b)
    

    1.逻辑非(Logical NOT Operator)

    let allowedEntry = false
    if !allowedEntry {
        print("ACCESS DENIED")
    }
    // Prints "ACCESS DENIED"
    

    2.逻辑与(Logical AND Operator)

    let enteredDoorCode = true
    let passedRetinaScan = false
    if enteredDoorCode && passedRetinaScan {
        print("Welcome!")
    } else {
        print("ACCESS DENIED")
    }
    // Prints "ACCESS DENIED"
    

    3.逻辑或(Logical AND Operator)

    let hasDoorKey = false
    let knowsOverridePassword = true
    if hasDoorKey || knowsOverridePassword {
        print("Welcome!")
    } else {
        print("ACCESS DENIED")
    }
    // Prints "Welcome!"
    

    4.组合逻辑运算符 (Combining Logical Operators)

    if enteredDoorCode && passedRetinaScan || hasDoorKey || knowsOverridePassword {
        print("Welcome!")
    } else {
        print("ACCESS DENIED")
    }
    // Prints "Welcome!"
    

    NOTE
    Swift逻辑运算符&&|| 是左关联的,这意味着具有多个逻辑运算符的复合表达式首先评估最左边的子表达式。

    5.显式圆括号 (Explicit Parentheses)

    if (enteredDoorCode && passedRetinaScan) || hasDoorKey || knowsOverridePassword {
        print("Welcome!")
    } else {
        print("ACCESS DENIED")
    }
    // Prints "Welcome!"
    

    相关文章

      网友评论

        本文标题:Swift4.2_操作运算符

        本文链接:https://www.haomeiwen.com/subject/pifrqqtx.html