• Swift--控制流与oc不同的地方


    1.For-in循环中...

    for index in 1...5 {

        print("(index) times 5 is (index * 5)")

    }

    for _ in 1...5 {

        可以用下划线忽略当前值

    }

    2.字典通过元祖返回

    3.do while循环变成repeat

    repeat {

        statements

    } while condition

    4.switch不需要break

    let someCharacter: Character = "z"

    switch someCharacter {

    case "a":

        print("The first letter of the alphabet")

    case "z":

        print("The last letter of the alphabet")

    default:

        print("Some other character")

    }

    5.switch case的body不能为空

    6.case可以带区间

    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).")

    7.case的元祖表示

    let somePoint = (1, 1)

    switch somePoint {

    case (0, 0):

        print("(0, 0) is at the origin")

    case (_, 0):

        print("((somePoint.0), 0) is on the x-axis")

    case (0, _):

        print("(0, (somePoint.1)) is on 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 of the box")

    }

    8.case加额外条件

    let 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")

    }

    9.case  fallthrough贯穿

    fallthrough关键字不会检查它下一个将会落入执行的 case 中的匹配条件。fallthrough简单地使代码继续连接到下一个 case 中的代码

    10.while加标签

    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!")

    11.guard与if的区别

    if语句一样,guard的执行取决于一个表达式的布尔值。我们可以使用guard语句来要求条件必须为真时,以执行guard语句后的代码。不同于if语句,一个guard语句总是有一个else从句,如果条件不为真则执行else从句中的代码。

    guard let name = person["name"] else {

        return

    }

    12.检测 API 可用性

    Swift内置支持检查 API 可用性,这可以确保我们不会在当前部署机器上,不小心地使用了不可用的API

    if #available(iOS 10, macOS 10.12, *) {

        // iOS 使用 iOS 10 API, macOS 使用 macOS 10.12 API

    } else {

        // 使用先前版本的 iOS macOS API

    }

    在它一般的形式中,可用性条件使用了一个平台名字和版本的列表。平台名字可以是iOSmacOSwatchOStvOS——请访问声明属性来获取完整列表。除了指定像 iOS 8的主板本号,我们可以指定像iOS 8.3 以及 macOS 10.10.3的子版本号。

     

     

  • 相关阅读:
    Git详解之四 服务器上的Git
    Git详解之三 Git分支
    Git详解之二 Git基础
    Git详解之一 Git起步
    Spring Data JPA 梳理
    Spring Data JPA 梳理
    Spring boot 梳理
    Apache和Tomcat 配置负载均衡(mod-proxy方式)-粘性session
    Apache和Tomcat 配置负载均衡(mod-proxy方式)-无session共享、无粘性session
    Spring boot 官网学习笔记
  • 原文地址:https://www.cnblogs.com/huoran1120/p/6118409.html
Copyright © 2020-2023  润新知