switch的用法

来源:互联网 发布:科荣软件 编辑:程序博客网 时间:2024/05/16 04:44

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 xwhere x.hasSuffix("pepper "):

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

    

    default :

    let vegetableComment ="Everything tastes good in soup."

}

运行 switch 中匹配到的子句之后,程序会退出 switch 语句,并不会继续向下运行,所以不 需要在每个子句结尾写 break。





0 0