let yetAnotherPoint = (1, -1)
switch yetAnotherPoint {
case let (x, y) where x == y:
println("((x), (y)) is on the line x == y")
case let (x, y) where x == -y:
println("((x), (y)) is on the line x == -y")
case let (x, y):
println("((x), (y)) is just some arbitrary point")
}
// prints "(1, -1) is on the line x == -y"
每个case都因为有where而不同,第一个case就是判断x是否与y相等,表示点在斜线y=x上。
4、控制跳转语句
在Swift中控制跳转语句有4种,让编程人员更好地控制代码的流转,包括:
continue
break
fallthrough
return
其中continue,break和fallthrough在下面详细介绍,return语句将在函数一章介绍。
continue
continue语句告诉一个循环停止现在在执行的语句,开始下一次循环。
注意:在for-condition-increment循环中,increment增量语句依然执行,只是略过了一次循环体。
下面的例子实现的是去除一个字符串中的空格和元音字母,从而组成一个字谜:
复制代码
let puzzleInput = "great minds think alike"
var puzzleOutput = ""
for character in puzzleInput {
switch character {
case "a", "e", "i", "o", "u", " ":
continue
default:
puzzleOutput += character
}
}
println(puzzleOutput)
// prints "grtmndsthnklk"
遍历字符串的每一个字符,当遇到元音字母或者空格时就忽略,进行下一次循环,从而得到了最终的字谜。
break
break语句将终止整个循环的执行,可以用在循环语句中,也可以用在switch语句中。









