iOS开发之Swift篇(5)—— 控制流

版本

Xcode 11.0
Swift 5.1

控制流

类似于其他语言, Switch中控制流关键字有:

  • if
  • guard
  • while
  • repeat-while
  • for (Swift 3开始遗弃, 使用for-in区间遍历)
  • for-in
  • switch

注: guard 的执行取决于一个表达式的布尔值, 类似于 if, 表过不提; repeat-while 类似于 do-while.

控制转移语句改变你代码的执行顺序,通过它可以实现代码的跳转。Swift 有五种控制转移语句:

  • continue
  • break
  • fallthrough
  • return
  • throw

注: 使用 fallthrough 关键字来“贯穿”跳转到下一个分支中; 使用 throw 来处理和抛出错误.

基于这些关键字的已知性/常用性/存异性, 下面挑选 for-in 和 switch 进行讨论.

for-in

let nameList = ["小黑", "小红", "小白", "小绿", "小蓝"]

// 全部遍历
for name in nameList {
    
    
    print(name)
}
/*
 小黑
 小红
 小白
 小绿
 小蓝
 */

// 区间遍历1 (全闭)
for index in 1...3 {
    
    
    print("\(nameList[index]) 的序号是 \(index)")
}
/*
 小红 的序号是 1
 小白 的序号是 2
 小绿 的序号是 3
 */

// 区间遍历2 (左闭右开)
for index in 1..<3 {
    
    
    print("\(nameList[index]) 的序号是 \(index)")
}
/*
 小红 的序号是 1
 小白 的序号是 2
 */

// 区间遍历3 (另一种写法)
for name in nameList[2...] {
    
        // 从下标2遍历到末尾
    print(name)
}
/*
 小白
 小绿
 小蓝
 */

// 区间遍历4 (倒序)
for index in (1...3).reversed() {
    
    
    print("\(nameList[index]) 的序号是 \(index)")
}
/*
 小绿 的序号是 3
 小白 的序号是 2
 小红 的序号是 1
 */

如果你不需要区间序列内每一项的值,你可以使用下划线(_)替代变量名来忽略这个值:

for _ in 1...3 {
    
    
    print("repeat")
}
/*
 repeat
 repeat
 repeat
 */

switch

1. 不必用 break
与 C 和 Objective-C 中的 switch 语句不同,在 Swift 中,当匹配的 case 分支中的代码执行完毕后,程序会终止 switch 语句,而不会继续执行下一个 case 分支。这也就是说,不需要在 case 分支中显式地使用 break 语句。当然你依然可以在 case 分支中的代码执行完毕前使用 break 跳出。

let age = 18

switch age {
    
    
case 18:
    print("小鲜肉")
default:
    print("老腊肉")
}
// 打印 小鲜肉

2. default必须放在最后
假如 default 之后还有 case , 那么编译的时候就会报错.

3. 不能两个case连用
switch 语句必须是完备的。这就是说,每一个可能的值都必须至少有一个 case 分支与之对应。
下面两个 case 连用, 报错:

switch age {
    
    
case 18:            
case 22:
    print("小鲜肉")
default:
    print("老腊肉")
}
// 报错, 因为case 18:后面没有代码句

如果需要同时匹配两个或两个以上条件, 使用","将它们分隔.
正解如下:

switch age {
    
    
case 18, 22, 25:
    print("小鲜肉")
default:
    print("老腊肉")
}
// 打印 小鲜肉

4. 区间匹配

switch age {
    
    
case 18...25:
    print("小鲜肉")
default:
    print("老腊肉")
}
// 打印 小鲜肉

5. 两个 case 均匹配, 只执行第一个

switch age {
    
    
case 18, 22, 25:
    print("小鲜肉1")
case 18...25:
    print("小鲜肉2")
default:
    print("老腊肉")
}
// 打印 小鲜肉1

6. switch 中使用元组
我们可以使用元组在同一个 switch 语句中测试多个值。元组中的元素可以是值,也可以是区间。另外,使用下划线(_)来匹配所有可能的值。

let somePoint = (1, 1)
switch somePoint {
    
    
case (0, 0):
    print("\(somePoint) is at the origin")
case (_, 0):
    print("\(somePoint) is on the x-axis")
case (0, _):
    print("\(somePoint) is on the y-axis")
case (-2...2, -2...2):
    print("\(somePoint) is inside the box")
default:
    print("\(somePoint) is outside of the box")
}
// 输出“(1, 1) is inside the box”

7. 绑定值
case 分支允许将匹配的值声明为临时常量或变量,并且在 case 分支体内使用 —— 这种行为被称为值绑定(value binding),因为匹配的值在 case 分支体内,与临时的常量或变量绑定。

let anotherPoint = (2, 0)
switch anotherPoint {
    
    
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 a y value of \(y)")
case let (x, y):
    print("somewhere else at (\(x), \(y))")
}
// 输出“on the x-axis with an x value of 2”

8. where
case 分支的模式可以使用 where 语句来判断额外的条件。

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")
}
// 输出“(1, -1) is on the line x == -y”

检测API可用性

if #available(平台名称 版本号, ..., *) {
    
    
    APIs 可用,语句将执行
} else {
    
    
    APIs 不可用,语句将不执行
}

// 例如
if #available(iOS 10, macOS 10.12, *) {
    
    
    // 在 iOS 使用 iOS 10 的 API, 在 macOS 使用 macOS 10.12 的 API
} else {
    
    
    // 使用先前版本的 iOS 和 macOS 的 API
}

猜你喜欢

转载自blog.csdn.net/u012078168/article/details/103682940
今日推荐