迅速有能力通过陈述吗?例如,如果我执行以下操作

var testVar = "hello"
var result = 0

switch(testVal)
{
case "one":
    result = 1
case "two":
    result = 1
default:
    result = 3
}

案例“一”和案例“二”是否可以执行相同的代码?

最佳答案

是。您可以按照以下方式进行操作:

var testVal = "hello"
var result = 0

switch testVal {
case "one", "two":
    result = 1
default:
    result = 3
}

另外,您可以使用fallthrough关键字:
var testVal = "hello"
var result = 0

switch testVal {
case "one":
    fallthrough
case "two":
    result = 1
default:
    result = 3
}

关于switch-statement - swift 落下,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24049024/

10-11 23:27
查看更多