为什么不允许快速切换开关盒的概念?
问题描述:
var index = 30
switch index {
case 10 :
println("Value of index is 10")
case 20 :
case 30 :
println("Value of index is either 20 or 30")
case 40 :
println("Value of index is 40")
default :
println("default case")
}
答
下通是允许在斯威夫特,但you do have to state it explicitly:
switch index {
case 10:
println("Value of index is 10")
case 20:
fallthrough
case 30:
println("Value of index is either 20 or 30")
...
因为虽然你的情况,它可能会更好,只是组的情况:
switch index {
case 10:
println("Value of index is 10")
case 20, 30:
println("Value of index is either 20 or 30")
...
答
或者你可以把它写这样( Swift 2.2语法):
switch index {
case 10:
print("Value is: \(index)")
case 20, 30:
print("Value is: \(index)")
default:
print("Default value is: \(index)")
}
此处使用贯穿的好例子http:// st ackoverflow.com/a/31782490/2303865 –