我想将我的值数组作为 switch 语句的案例
我有一个值数组
let intValues = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 .... 100]
我想使用 switch 语句作为比较工具,如下所示。
let inputValue = 30 // or some other int value
switch inputValue {
case 101:
// do something lol
/* =============================== */
case intValues: //
// do something more:
/* =============================== */
case 13131:
// do another thing
default:
// do default
}
我知道我可以通过这样做轻松做到这一点case 1, 2, 3, 4, 5,
等等或通过使用if 语句像这样:
if intValues.contains(inputValue) {
// do something more:
} else if inputValue == 101 {
// do something lol
} else if inputValue == 13131 {
// do another thing
} else {
// do default
}
或者通过做类似的事情
if intValues.contains(inputValue) {
// do something more:
} else {
switch inputValue {
case 101:
// do something lol
case 13131:
// do another thing
default:
// do default
}
}
反之亦然
但这可能吗?只需一个 switch 语句就可以做到这一点?