为什么在swift中不允许使用开关盒的概念?

KSR*_*KSR 0 switch-statement ios swift

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")
}
Run Code Online (Sandbox Code Playgroud)

sam*_*m-w 5

Swift中允许使用Fallthrough,但您必须明确说明它:

switch index {
case 10:
    println( "Value of index is 10")
case 20:
    fallthrough
case 30:
    println( "Value of index is either 20 or 30")
...
Run Code Online (Sandbox Code Playgroud)

但就你的情况而言,将你的案例分组可能更好:

switch index {
case 10:
    println( "Value of index is 10")
case 20, 30:
    println( "Value of index is either 20 or 30")
...
Run Code Online (Sandbox Code Playgroud)