a switch bug in swift? - “Switch must be exhaustive, consider adding a default clause.”

前端 未结 3 859
青春惊慌失措
青春惊慌失措 2020-12-21 04:11

I am positive that the following swift code has covered all possibilities, but Xcode keeps telling me that, \"Switch must be exhaustive, consider ad

3条回答
  •  遥遥无期
    2020-12-21 04:49

    In Swift, a switch statement must always provide an option for all possible cases. If you have an enum, you can add all enum values and the switch will be exhaustive. If it is not exhaustive, you need to add a default case, this will trigger when no other case is matched.

    If you are switching on a variable, you should exhaust all cases. If you do that, a default case is not needed.

    A programmer might be able to see that this switch is exhaustive, but the compiler does not. That is why you get the error, and you can fix it by adding a default case.

提交回复
热议问题