本文介绍了迅速落案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

swift 是否有fall through 声明?例如,如果我执行以下操作

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
}

这篇关于迅速落案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 20:13