美文网首页
Swift 控制流

Swift 控制流

作者: 点滴86 | 来源:发表于2016-08-04 20:01 被阅读4次

    For - in 循环

    import UIKit
    
    // For - in 循环
    // 闭区间
    for index in 1...5 {
        print("\(index) times 5 is \(index * 5)")
    }
    
    // 数组
    var namesArray = ["Jordan", "Kobe", "Wade"]
    for name in namesArray {
        print("Hello, \(name)!")
    }
    
    // 字典
    var numberOfLegs = ["spider" : 8, "ant" : 6, "cat" : 4]
    for (animalName, legCount) in numberOfLegs {
        print("\(animalName)s have \(legCount) legs")
    }
    

    console log 如下


    For-in循环.png

    while 循环

    // while 循环 (先判断条件,在执行语句)
    var i = 0
    while i < 10 {
        i += 1
        print("While 第\(i)次循环")
    }
    

    console log 如下


    while循环.png

    repeat - while 循环

    // repeat - while 循环 (先执行语句,在判断条件)
    var j = 11
    repeat {
        j += 1
        print("repeat While 第\(j)次循环")
    } while j < 10
    

    console log 如下


    repeat-while循环.png
    if 条件语句
    // if 条件语句
    var temperaturInFahrenheit = 40
    if temperaturInFahrenheit < 32 {
        print("It's very cold. Consider wearing a scarf.")
    } else {
        print("It's not that cold. Wear a t-shirt")
    }
    

    console log 如下


    if条件语句.png

    switch 常规用法(case 分支里面必须有语句,默认只执行一个分支,不用写break)

    // switch 常规用法
    var someCharacter : Character = "a"
    switch someCharacter {
    case "a", "A" :
        print("The letter a")
    case "b" :
        print("The letter b")
    default :
        print("The other letter")
    }
    

    console log 如下


    switch常规用法.png

    switch 区间匹配

    // switch 区间匹配
    let approximateCount = 62
    let countedThings = "moons orbiting Saturn."
    var naturalCount : String
    switch approximateCount {
    case 0:
        naturalCount = "no"
    case 1..<5:
        naturalCount = "a few"
    case 5..<12:
        naturalCount = "several"
    case 12..<100:
        naturalCount = "dozens of"
    case 100..<1000:
        naturalCount = "hundreds of"
    default:
        naturalCount = "many"
    }
    
    print("There are \(naturalCount) \(countedThings)")
    

    console log 如下


    switch区间匹配.png

    switch 元组匹配

    // switch 元组匹配
    var somePoint = (1, 1)
    
    switch somePoint {
    case (0, 0):
        print("(0, 0) is at the origin")
    case (_, 0):
        print("(\(somePoint.0), 0) is at the x-axis")
    case (0, _):
        print("(0, \(somePoint.1)) is at the y-axis")
    case (-2...2, -2...2):
        print("(\(somePoint.0), \(somePoint.1)) is inside the box")
    default:
        print("(\(somePoint.0), \(somePoint.1)) is outside the box")
    }
    

    console log 如下


    switch 元组匹配.png

    switch 元组值绑定

    // switch 元组值绑定
    var otherPoint = (1, 5)
    switch otherPoint {
    case (let x, 0):
        print("on the x-axis with an x value of \(x)")
    case (0, let y):
        print("on the y-axis with an y value of \(y)")
    case (let x, let y):
        print("somewhere else at (\(x), \(y))")
    }
    

    console log 如下


    switch 元组值绑定.png

    switch 元组值绑定where 筛选

    // switch 元组值绑定where 筛选
    var yetAnotherPoint = (1, 1)
    switch yetAnotherPoint {
    case let (x, y) where x == y:
        print("(\(x), \(y)) is on the line x == y")
    case let (x, y) where x == -y:
        print("(\(x), \(y)) is on the line x == -y")
    case let (x, y):
        print("(\(x), \(y)) is just some arbitrary point")
    }
    

    console log 如下


    switch 元组值绑定where 筛选.png

    continue 关键字

    // continue 关键字
    let puzzleInput = "great minds think alike"
    var puzzleOutput = ""
    for character in puzzleInput.characters {
        switch character {
            case "a", "e", "i", "o", "u", " ":
            continue
        default :
            puzzleOutput.append(character)
        }
    }
    
    print(puzzleOutput)
    

    console log 如下


    continue 关键字.png

    fallthrough 关键字

    // 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)
    

    console log 如下


    fallthrough 关键字.png

    相关文章

      网友评论

          本文标题:Swift 控制流

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