swift case falling through

前端 未结 5 883
时光说笑
时光说笑 2020-12-07 21:30

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         


        
5条回答
  •  佛祖请我去吃肉
    2020-12-07 22:10

    Here is example for you easy to understand:

    let value = 0
    
    switch value
    {
    case 0:
        print(0) // print 0
        fallthrough
    case 1:
        print(1) // print 1
    case 2:
        print(2) // Doesn't print
    default:
        print("default")
    }
    

    Conclusion: Use fallthrough to execute next case (only one) when the previous one that have fallthrough is match or not.

提交回复
热议问题