Swift学习笔记switch

swift 的switch加入了scala里的模式匹配功能,比C#的switch更为强大,但我相信,C#在后续版本肯定会加入这些功能:

let count = 300
let countedThings = "starts"

var naturalCount:String
switch count {
case 0:
    naturalCount = "no"
case 100...300:
    naturalCount = "yes"
default:
    naturalCount = "a"
}

let somePoint = (1,1)
switch somePoint{
case (0,0):
    naturalCount = "no"
case (1,_):
    naturalCount = "yes"
default:
    naturalCount = ""
}

switch somePoint{
case (0,0):
    naturalCount = "no"
case (1,let x):
    naturalCount =  String(x)
default:
    naturalCount = ""
}

switch somePoint{
case let (x,y) where x == y:
    naturalCount = "no"
case let (x,y) where x != y:
    naturalCount =  String(x)
default:
    naturalCount = ""
}

这是三种switch 功能,scala是去掉了switch关键字,使用模式匹配替代掉了,而switch保留了这个控制语句

还保留了对于C 的switch语法支持,与Break语句,这里仅仅是个人笔记,所以不展示了。

你可能感兴趣的:(switch)