美文网首页
更加强大的switch

更加强大的switch

作者: 一个栗 | 来源:发表于2021-06-20 11:31 被阅读0次
  • switch语句会将一个值与多个可能的模式匹配。然后基于第一个成功匹配的模式来执行合适的代码块。
  • switch语句一定是全面的。就是说,给定类型里每一个值都得被考虑到并且匹配到一个 switch 的 case。如果无法提供一个switch case 所有可能的值,你可以定义一个默认匹配所有的 case 来匹配所有未明确出来的值。这个匹配所有的情况用关键字 default 标记,并且必须在所有 case 的最后出现。
  • Object-C switch 语句如果不全面,仍然可以运行;但 Swift 不行。

没有隐式贯穿

  • 相比 C 和 Object-C 里的 switch 语句来说,Swift 里的 switch 语句不会默认从匹配的 case 的末尾贯穿到下一个 case 里。
  • 相反,整个 switch 语句会在匹配到第一个 switch 的 case 执行完毕之后退出,不再需要显示的 break 语句。
    C 代码
char c = 'z';
    switch (c) {
        case 'a':
            NSLog(@"the first");
        case 'z':
            NSLog(@"the last");
        default:
            NSLog(@"other");
    }

运行结果如下:
the last
other

Swift 代码

let someCahracter:Character = "z"
switch someCahracter {
case "a":
    print("the first")
case "z":
    print("the last")
default:
    print("other")
}

运行结果如下:
the last
  • 每一个 case 的函数体必须包含至少一个可执行的语句。
  • 在一个 switch 的 case 中匹配多个值可以用逗号分隔,并且可以写成多行。


    switch case .png
let someCahracter:Character = "a"
switch someCahracter {
case "a","A":
    print("the first")
case "z":
    print("the last")
default:
    print("other")
}

运行结果如下:
the first

区间匹配

  • swich 的 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).")

运行结果如下:
there are dozens of moons orbiting Saturn.

元组匹配

  • 你可以使用元组来在一个 swich 语句中测试多个值。
  • 使用下划线(_)来表明匹配所有可能的值
let 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.0)) is at the y-axis")
case (-2...2, -2...2):
        print("(\(somePoint.0),\(somePoint.1)) is inside of the box")
default:
    print("(\(somePoint.0),\(somePoint.1)) is outside of the box")
}

运行结果如下:
(1,1) is inside of the box

值绑定

  • switch 的 case 可以将匹配到的值临时绑定为一个常量或者变量,来给 case 的函数使用。
  • 如果使用 var 关键字,临时的变量就会以合适的值来创建并初始化。对这个变量的任何改变都只会在 case 的函数体内有效。
let anotherPoint = (2,1)
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 an y value of \(y)")
case let(x, y):
    print("somewhere else at (\(x),\(y))")
}

运行结果如下:
somewhere else at (2,1)

where 字句

  • switch 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

复合匹配

  • 多种情形共享同一个函数体的多个情况可以在case 写多个模式来复合,在每个模式之间用逗号分隔。如果任何一个模式匹配了,那么这个情况都会被认为是匹配的。如果模式太长,可以把他们写成多行
let someCahracter:Character = "e"
switch someCahracter {
case "a","e","i","o","u":
    print("\(someCahracter) 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("\(someCahracter) is a consonant")
default:
    print("\(someCahracter) is not a vowel or a consonant")
}

运行结果如下:
e is a vowel

复合匹配-值绑定

  • 复合匹配同样可以包含值绑定。所有复合匹配的模式都必须包含相同的值绑定集合,并且符合情况中的每一个绑定都得有相同的类型模式。这才能确保无论复合匹配的那部分命中了,接下来的函数体中的代码都能访问到绑定的值并且值得类型也都相同。
let stillAnotherPoint = (9,0)
switch stillAnotherPoint {
case (let distance, 0),(0, let distance) :
    print("on an sxis, \(distance) from the origin")
default:
    print("Not on an axis")
}

运行结果如下:
on an sxis, 9 from the origin

相关文章

  • 更加强大的 switch

    switch switch 语句会将一个值与多个可能的模式匹配。然后基于第一个成功匹配的模式来执行合适的代码块 s...

  • 更加强大的switch

    switch语句会将一个值与多个可能的模式匹配。然后基于第一个成功匹配的模式来执行合适的代码块。 switch语句...

  • kotlin基础之三 流程控制,函数

    1.when表达式 when取代了C风格语言的switch。相较switch而言,when语句显得更加的强大,灵活...

  • Kotlin常用语法

    java中的switch kotlin使用when代替java中的switch,但比switch强大的多 for循环

  • scala中的match详解

    Scala中提供了比java,c更加强大的switch语句——match,而且不用担心因为忘记写break语句而发...

  • scala(二十一) 模式匹配(match)

    前言 Scala中的模式匹配类似于Java中的switch语法,但是更加强大。模式匹配语法中,采用match关键字...

  • scala-模式匹配和样例类

    模式匹配和样例类 模式匹配 Scala没有Java中的switch case,它有一个更加强大的模式匹配机制,可以...

  • Swift 基本语法(三)— 循环控制

    swift 循环控制 强大的switch 语句 switch 必须有一个默认匹配default。 并且没有隐式...

  • Swift中的模式匹配

    Swift里的switch比OC里面强大很多,switch的主要特性就是模式匹配。下面先举个非常简单的例子。 看完...

  • Swift中强大的模式匹配

    Swift中的模式匹配语法是一个亮点,Swift里switch比OC里面强大很多,switch的主要特性就是模式匹...

网友评论

      本文标题:更加强大的switch

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