美文网首页Swift
Swift 中的 switch case

Swift 中的 switch case

作者: 孤雁_南飞 | 来源:发表于2021-01-14 09:10 被阅读0次
  1. switch 语句会将一个值与多个可能的模式匹配,然后基于第一个成功匹配的模式来执行合适的代码块
  2. switch 语句一定得是全面的,就是说,给定类型里每一个值都会被考录到并且匹配到一个 switch 的 case 如果无法提供一个 switch case 所有可能的值,你可以定义一个默认匹配所有的 case 来匹配所有未明确出来的值,这个匹配所有的情况用关键字 default 标记,并且必须在所有 case 的最后出现
  • 没有隐式贯穿
  1. 相比 C 和 Objective-C 离得 switch 语句来说,Swift 里的 switch 语句不会默认从匹配 case 的末尾贯穿到下一个 case 里
  2. 相反,整个 switch 语句会在匹配到第一个 switch 的 case 执行完毕后退出,不在需要显式的 break 语句
let someCharacter: Character = "z"
switch someCharacter {
case "a":
    print("The first letter of the alphabet")
case "z":
    print("The last latter of alphabet")
default:
    print("Some other character")
}

3.每一个 case 的函数体必须包含至少一个可执行语句
4.在一个 switch 的 case 中匹配多个值可以用都好分隔,并且可以写成多行

switch someCharacter {
case "a", "A":
    print("The latter A")
default:
    print("Not the letter A")
}
switch someCharacter {
case "a",
     "A":
    print("The latter A")
default:
    print("Not the letter A")
}

5.区间匹配:switch 的 case 的值可以再一个区间中匹配

let approximateCount = 42
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) moons orbiting Saturn")

6.元组匹配:你可以使用元组来在一个 switch 语句中测试多个值,使用下划线(_)来标明匹配所有可能的值

let somePoint = (1, 1)
switch somePoint {
case (0, 0):
    print("(0, 0) is at the origin")
case (_, 0):
    print("(\(somePoint.0), 0) is one the x-axis")
case (0, _):
    print("(0, \(somePoint.1)) is one 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")
}

7.值绑定: switch 的 case 可以将匹配到的值淋湿绑定为一个常量或者变量,来给case 的函数体使用
8.如果使用 var 关键字,临时的变量就会以合适的值来创建并初始化,对这个变量的任何改变都只会在 case 的函数体内有效

let somePoint = (2, 0)
switch somePoint {
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, y):
    print("somewhere else at (\(x), \(y)")
}

9.where 语句: switch case 可以使用 where 分句来检查是否符合特定的约束

let somePoint = (1, -1)
switch somePoint {
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")
}

10.复合匹配: 多种清醒共享同一个函数体的多种情况,可以在 case 后写多个模式来复合,在每个模式之间用都好分隔,如果任意一个模式匹配了,那么这个情况都会被认为是匹配的,如果模式太长,可以把他们写成多行

let someCharacter: Character = "e"
switch someCharacter {
case "a", "e", "i", "o", "u":
    print("\(someCharacter) is a vowel")
case "b", "c", "d", "f", "g", "h", "j", "k", "l", "m",
     "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z":
    print("\(someCharacter) is a consonant")
default:
    print("\(someCharacter) is not a vowel or a consonant")
}

11.复合匹配-值绑定:复合匹配同样可以包含值绑定,所有复合匹配的模式都必须包含相同的值绑定集合,并且复合情况中的每一个绑定都得有相同的类型格式,这才能确保无论复合匹配的那部分命中了,接下来的函数体重的代码都可以访问到绑定的值,并且值的类型也都相同

let stillAnotherPoint = (9, 0)
switch stillAnotherPoint {
case (let distance, 0), (0, let distance):
    print("On an axis, \(distance) from the origin")
default:
    print("Not on an axis")
}

- Swift 里的控制转移

  • continue、break、fallthrough、return、throw
  1. continue 语句告诉循环停止正在做的事情,并且再次从头开始循环的下一次遍历,他的意思是:我不再继续当前的循环遍历了,而不是离开整个循环
  2. break语句会立即结束整个控制语句,当你想要提前结束 switch 或者循环语句 或者其他情况时,可以再 switch 语句或者循环语句中使用 break 语句
  3. 当在循环语句中使用时,break 会立即结束循环的执行,并且转移控制到循环结束花括号(})后的第一行代码上,当前遍历循环里的其他代码都不会被执行,并且余下的遍历循环也不会开始了
  4. 当在 switch 语句里使用时,break 导致 switch语句立即结束他的执行,并且转移控制到 switch 语句结束花括号(})之后的第一行代码上
  5. fallthrough:如果你确实需要 C 或者 Objective-C 风格的贯穿行动,你可以选择在 switch 每个 case 末尾使用fallthrough
let integerToDescribe = 5
var description = "The number \(integerToDescribe) is"
switch integerToDescribe {
case 1, 3, 5, 7, 11, 13, 17, 19:
    description += "a prime number, and also"
    fallthrough
default:
    description += " an integer"
}
print(description)

6.语句标签:可以用语句标签来给循环语句或者条件语句做标记,在一个条件语句中,你可以使用一个语句标签配合break语句来结束被标记的语句,在循环语句中,你可以使用语句标签来配合 break 或者 continue 语句来结束或者继续执行被标记的语句

var num = 10
whileLoop : while num > 0 {
    switch num {
    case 9:
        print("9")
    case 10:
        var sum = 0
        for index in 0...10 {
            sum += index
            if index == 9 {
                print(sum)
                break whileLoop
            }
        }
    default:
        break
    }
    num -= 1
}

相关文章

网友评论

    本文标题:Swift 中的 switch case

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