swift case falling through(快速案件落空)
问题描述
swift 有没有通过语句?例如,如果我执行以下操作
Does swift have fall through statement? e.g if I do the following
var testVar = "hello"
var result = 0
switch(testVal)
{
case "one":
result = 1
case "two":
result = 1
default:
result = 3
}
是否可以为案例一"和案例二"执行相同的代码?
is it possible to have the same code executed for case "one" and case "two"?
推荐答案
是的.你可以这样做:
var testVal = "hello"
var result = 0
switch testVal {
case "one", "two":
result = 1
default:
result = 3
}
或者,您可以使用 fallthrough
关键字:
Alternatively, you can use the fallthrough
keyword:
var testVal = "hello"
var result = 0
switch testVal {
case "one":
fallthrough
case "two":
result = 1
default:
result = 3
}
这篇关于快速案件落空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!