Noop for Swift's Exhaustive Switch Statements

后端 未结 5 1635
有刺的猬
有刺的猬 2020-11-30 07:17

Swift requires exhaustive switch statements, and that each case have executable code.

\'case\' label in a \'switch\' should have at least one executable

5条回答
  •  猫巷女王i
    2020-11-30 07:34

    The cleanest solution I've found is to simply include your last statement in the switch case as your default. This avoids the need to add break or other unnecessary statements while still covering all possible cases.

    For example:

    switch myVar {
    
    case 0:
        myOtherVar = "Red"
    
    case 1:
        myOtherVar = "Blue"
    
    default:
        myOtherVar = "Green"
    
    }
    

提交回复
热议问题