Swift requires exhaustive switch statements, and that each case have executable code.
'case' label in a 'switch' should have at least one executable statement
Has anybody settled on a good way to handle the cases where you don't want to actually do anything? I can put a println() in there, but that feels dirty.
The section about control flow of the language guide says: Every switch statement must be exhaustive. That is, every possible value of the type being considered must be matched by one of the switch cases.
The switch statement in Swift lets you inspect a value and match it with a number of cases. It's particularly effective for taking concise decisions based on one variable that can contain a number of possible values. Using the switch statement often results in more concise code that's easier to read.
Although break isn't required in Swift, you can use a break statement to match and ignore a particular case or to break out of a matched case before that case has completed its execution. For details, see Break in a Switch Statement. The body of each case must contain at least one executable statement.
According to the book, you need to use break
there:
The scope of each case can’t be empty. As a result, you must include at least one statement following the colon (:) of each case label. Use a single
break
statement if you don’t intend to execute any code in the body of a matched case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With