Noop for Swift's Exhaustive Switch Statements

后端 未结 5 1636
有刺的猬
有刺的猬 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条回答
  •  没有蜡笔的小新
    2020-11-30 07:57

    In addition to break mentioned in other answers, I have also seen () used as a no-op statement:

    switch 0 == 1 {
    case true:
        break
    case false:
        ()
    }
    

    Use () if you find break confusing or want to save 3 characters.

提交回复
热议问题