Welcome to Swift (苹果官方Swift文档初译与注解二十七)---189~198页(第四章-- 流程控制)

Switch

  一个switch语句里包含一个值,并且用这个值与其他几个可能的匹配模式进行比较,然后根据成功匹配上的模式,执行相应的代码块.switch语句提供了比if语句更多的选项来相应多种潜  在的情况.

  最简单的一个例子:

    switch some value to consider {

    case value 1:

         respond to value 1

    case value 2,

    value 3:

        respond to value 2 or 3

    default:

        otherwise, do something else

    }

  每一个switch语句都包含多个case关键字.另外为了比较指定的值,Swift针对每个case提供了几种不同的匹配模式,这些将在后面的部分进行说明.

  每个switch的case体都是一个独立的代码执行分支,这与if语句的分支有点类似.switch语句决定哪一个分支会被选中执行.

  switch语句必须是周全的,也就是每种可能的值都会匹配到一个case.如果不是这样,你可以通过关键字default来定义一个默认的case,它默认总是出现在最后面.

  下面的例子使用了switch语句来处理一个小写字符(someCharacter):

    let someCharacter: Character = "e"

    switch someCharacter {

    case "a", "e", "i", "o", "u":

        println("\(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":

         println("\(someCharacter) is a consonant")

    default:

        println("\(someCharacter) is not a vowel or a consonant")

    }

    // prints "e is a vowel”

  这个switch语句的第一个case匹配了英语里所有的五个元音,类似的,第二个case匹配了英语中其他小写字母.

  在一个switch的case中写尽其他所有的可能性是不实际的,因此switch语句提供了一个default的case用来匹配其他情况,这样保证switch语句尽量穷尽.

No Implicit Fall through (非默认降落执行)

  与C语言和OC中的switch语句比较,在Swift中switch语句不会在每个case语句后默认的掉落到后面下一个case,相反的,整个switch语句在第一个匹配的case执行完成之后就结束了.  这使得switch比C语言的更加安全和清晰,避免了多个case的执行错误.

  注意点:

  你仍可以在一个匹配的case中,在这个case执行完成之前,提前跳出,如果你需要这样的话.

  每个case比较包含至少一个可以执行的语句,下面代码的写法是错误的,因为第一个case是空的:

    let anotherCharacter: Character = "a"

    switch anotherCharacter {

    case "a":

    case "A":

        println("The letter A")

    default:

        println("Not the letter A")

    }

    // this will report a compile-time error

  不像C语言的switch语句那样不匹配"a" 和 "A",相反编译器会报告错误,case “a”:没有包含任何执行语句.这个方法避免了从一个case掉落的另一个case,使得代码更加安全和干     净.

  在一个switch的case中有多个匹配值可以使用逗号(,)分隔,也可以分成多行来写:

    switch some value to consider {

    case value 1,

    value 2:

         statements

    }

  注意点:

  为了使一个特定的switch可以有掉落的功能,可以使用关键字fallthrough.

Range Matching (范围匹配)

  在switch的case中可以检查值是否包含在一个范围.下面的例子使用数字范围来提供了自然语言的关于任意大小数字的情况:

    let count = 3_000_000_000_000

    let countedThings = "stars in the Milky Way"

    var naturalCount: String

    switch count {

    case 0:

        naturalCount = "no"

    case 1...3:

        naturalCount = "a few"

    case 4...9:

        naturalCount = "several"

    case 10...99:

         naturalCount = "tens of"

    case 100...999:

        naturalCount = "hundreds of"

    case 1000...999_999:

        naturalCount = "thousands of"

    default:

        naturalCount = "millions and millions of"

    }

    println("There are \(naturalCount) \(countedThings).")

    // prints "There are millions and millions of stars in the Milky Way.”

Tuples (元组)

  可以在同一个switch语句中使用元组来测试多个值.元组中的每一个元素都可以用来测试不同的值或者范围.也可以使用下划线(_)标识出任意可能的匹配值.

  下面的例子中,使用与点(x,y)使用一个简单的(Int,Int)类型元组来表示,并它在图形上的位置:

    let somePoint = (1, 1)

    switch somePoint {

    case (0, 0):

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

    case (_, 0):

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

    case (0, _):

        println("(0, \(somePoint.1)) is on the y-axis")

    case (-2...2, -2...2):

        println("(\(somePoint.0), \(somePoint.1)) is inside the box")

    default:

        println("(\(somePoint.0), \(somePoint.1)) is outside of the box")

    }

    // prints "(1, 1) is inside the box”

    “let somePoint = (1, 1)

    switch somePoint {

    case (0, 0):

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

    case (_, 0):

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

    case (0, _):

        println("(0, \(somePoint.1)) is on the y-axis")

    case (-2...2, -2...2):

        println("(\(somePoint.0), \(somePoint.1)) is inside the box")

    default:

        println("(\(somePoint.0), \(somePoint.1)) is outside of the box")

    }

    // prints "(1, 1) is inside the box”

    Welcome to Swift (苹果官方Swift文档初译与注解二十七)---189~198页(第四章-- 流程控制)

  例子中,switch语句判断这个点是否在原点(0,0),是否在红色的X轴,是否在橘色的Y轴;是否在蓝色的4*4的盒子内,或者在盒子外.

  不像C语言中的switch允许多个case可以处理同一个值.实际上,上面例子中原点(0,0)可以匹配所有的四种情况,然而如果多种匹配,那么第一个匹配的case总是被选中,因此点(0,0)将  会匹配到case (0, 0),因此所有其他的匹配都会被忽略.

 

Welcome to Swift (苹果官方Swift文档初译与注解二十七)---189~198页(第四章-- 流程控制),布布扣,bubuko.com

Welcome to Swift (苹果官方Swift文档初译与注解二十七)---189~198页(第四章-- 流程控制)

上一篇:结果集一组数据的第几条ROW_NUMBER基本用法


下一篇:SQL 语句修改列名 属性 默认值