您的位置:首页 > 其它

switch的用法

2016-01-28 14:45 316 查看
switch 支持任意类型的数据以及各种比较操作——不仅仅是整数以及测试相等。

let vegetable = "red pepper"

switch vegetable {

case "name":

let vegetableComment =
"Add some raisins and make ants on a log."

case
"cucumber", "watercress":

let vegetableComment =
"That would make a good tea sandwich."

case let x
where x.hasSuffix("pepper "):

let vegetableComment =
"Is it a spicy \(x)?"

default :

let vegetableComment =
"Everything tastes good in soup."

}

运行 switch 中匹配到的子句之后,程序会退出 switch 语句,并不会继续向下运行,所以不 需要在每个子句结尾写 break。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: