美文网首页iOS Developer
swift入门4 控制流

swift入门4 控制流

作者: C93zzo | 来源:发表于2017-04-11 16:58 被阅读68次

    注:英文部分来自官方文档

    switch

    • fallthrough

    In contrast with switch statements in C and Objective-C, switch statements in Swift do not fall through the bottom of each case and into the next one by default. Instead, the entire switch statement finishes its execution as soon as the first matching switch case is completed, without requiring an explicit break statement. This makes the switch statement safer and easier to use than the one in C and avoids executing more than one switch case by mistake.

    与c 和oc相比,swift中 的switch并不会默认的从一个case跳转到下一个case。而是整个switch语句只要找到匹配的case就会完成执行过程,并不需要一个显式的break声明。

    If you need C-style fallthrough behavior, you can opt in to this behavior on a case-by-case basis with the fallthrough keyword. The example below uses fallthrough to create a textual description of a number.

    如果你需要使用c风格的fallthrough 行为,你可以在case后面加上fallthrough关键字。

    let integerToDescribe = 5
    var description = "The number \(integerToDescribe) is"
    switch integerToDescribe {
    case 2, 3, 5, 7, 11, 13, 17, 19:
        description += " a prime number, and also"
        fallthrough
    default:
        description += " an integer."
    }
    print(description)
    // Prints "The number 5 is a prime number, and also an integer."
    

    Although break is not required in Swift, you can use a break statement to match and ignore a particular case or to break out of a matched case before that case has completed its execution.

    虽然break在swift中不是必须的,但是你依然可以使用break声明来匹配或忽略那些特定的case,或者在匹配的case完成操作前从中退出。

    The body of each case must contain at least one executable statement. It is not valid to write the following code, because the first case is empty:
    每个case的内部必须包含至少一个可执行的语句。下面的代码中,第一个case是空的,所以是无效的:

    let anotherCharacter: Character = "a"
    switch anotherCharacter {
    case "a": //无效,该case内部为空
    case "A":
        print("The letter A")
    default:
        print("Not the letter A")
    }
    //这会报一个编译时错误.
    

    To make a switch with a single case that matches both "a" and "A", combine the two values into a compound case, separating the values with commas.

    为了使switch能在一个case同时匹配a 和 A, 可以把两个值组合到一个case里,用逗号隔开:

    let anotherCharacter: Character = "a"
    switch anotherCharacter {
    case "a", "A":
        print("The letter A")
    default:
        print("Not the letter A")
    }
    // Prints "The letter A"
    
    • Labeled Statements

    In Swift, you can nest loops and conditional statements inside other loops and conditional statements to create complex control flow structures. However, loops and conditional statements can both use the break statement to end their execution prematurely. Therefore, it is sometimes useful to be explicit about which loop or conditional statement you want a break statement to terminate. Similarly, if you have multiple nested loops, it can be useful to be explicit about which loop the continue statement should affect.

    在swift中,你可以在别动循环和条件语句中嵌套循环和条件语句,以此创造复杂的控制流结构。但是,循环和条件语句都可以用break来结束操作。因此,显式地标明哪一个循环或条件语句是你想要结束的就很有用。同样地,如果你有多个嵌套的循环,显式的标明continue语句作用于哪个循环也很有用

    To achieve these aims, you can mark a loop statement or conditional statement with a statement label. With a conditional statement, you can use a statement label with the break statement to end the execution of the labeled statement. With a loop statement, you can use a statement label with the break or continue statement to end or continue the execution of the labeled statement.

    为了达到这一目标,你可以用一个语句标签(statement label)来标记一个循环或者条件语句。对于条件语句,你可以使用一个带有break语句的语句标签来结束这个被标记的语句。对于循环语句,你可以使用带有break或continue语句的语句标签来结束或跳过这个被标记的语句。
    下面是一个while循环的例子,对于其他的循环和switch语句,语法规则一样适用。

    label name: while condition {
        statements
    }
    

    更完整的例子

    let finalSquare = 25
    var board = [Int](repeating: 0, count: finalSquare + 1)
    board[03] = +08; board[06] = +11; board[09] = +09; board[10] = +02
    board[14] = -10; board[19] = -11; board[22] = -02; board[24] = -08
    var square = 0
    var diceRoll = 0
    gameLoop: while square != finalSquare {
        diceRoll += 1
        if diceRoll == 7 { diceRoll = 1 }
        switch square + diceRoll {
        case finalSquare:
            // diceRoll will move us to the final square, so the game is over
            break gameLoop
        case let newSquare where newSquare > finalSquare:
            // diceRoll will move us beyond the final square, so roll again
            continue gameLoop
        default:
            // this is a valid move, so find out its effect
            square += diceRoll
            square += board[square]
        }
    }
    print("Game over!")
    

    如果你觉得文章不错,可以给我打赏点比特股(bts),以示支持。_

    BTS6jUaVVkz9gN8t9sWY9NR5UbiubSz7QtVDnEtFGpujYeqQSfQ5E

    相关文章

      网友评论

        本文标题:swift入门4 控制流

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