swift case falling through

前端 未结 5 875
时光说笑
时光说笑 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:02

    Yes. You can do so as follows:

    var testVal = "hello"
    var result = 0
    
    switch testVal {
    case "one", "two":
        result = 1
    default:
        result = 3
    }
    

    Alternatively, you can use the fallthrough keyword:

    var testVal = "hello"
    var result = 0
    
    switch testVal {
    case "one":
        fallthrough
    case "two":
        result = 1
    default:
        result = 3
    }
    

提交回复
热议问题