<分区>
我想将值数组作为 switch 语句的 case
我有一组值 say
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 语句就可以做到这一点?