Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Noop for Swift's Exhaustive Switch Statements

Tags:

swift

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.

like image 996
Sean McMains Avatar asked Jun 10 '14 13:06

Sean McMains


People also ask

What does switch must be exhaustive mean?

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.

What is a switch statement in Swift?

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.

Do Swift switch statements need break?

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.


1 Answers

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.

like image 141
Sergey Kalinichenko Avatar answered Sep 25 '22 17:09

Sergey Kalinichenko