swift switch语句

switch选择
1)case多条件匹配:条件之间用逗号隔开
用三个点表示范围:…,..<:表示不包含上边界
var tand = 1

switch tand{
    case 0:
        println(tand)
    case 2,3,5:
        println(tand)
    case 6...10:
        println(tand)
    default :
        println(tand)
}
 
2)数值绑定
 
let point = (10,1)
switch point{
//和point结构相同的,用来匹配传进来的值
case (let x , 0):
    println("x = \(x)")
case (0,let y):
    println("y = \(y)")
case (let x,let y):
    println("x = \(x) y = \(y)")
}
 
3)匹配元组
case (0 , 0):
case (_ , 0):  //忽略第一个元素
 
 
4)使用where来增加判断条件
 
5)fallthrough:执行完当前case,继续向下执行
注意:fallthrough后面的case条件不能定义变量和常量
switch point{
case (0...3, 6...10):
    println("\(point)")
case let ( x , y ) where x == y :
    println("\(point)")
    fallthrough
default:
    println("0")
}
输出结果:
(5, 5)
0
 
注意:
1、每一个case后面必须有一个可以执行的语句
2、匹配类型:还可以是字符串
posted @ 2016-05-04 14:30  bonjour520  阅读(255)  评论(0编辑  收藏  举报