Noop for Swift's Exhaustive Switch Statements

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.


You can use a break statement:

let vegetable = "red pepper"
var vegetableComment: String = "Nothing"
switch vegetable {
case "cucumber", "watercress":
    break // does nothing
case let x where x.hasSuffix("pepper"):
    vegetableComment = "Is it a spicy \(x)?"
default:
    vegetableComment = "Everything tastes good in soup."
}

Example modified from the docs


Below is one option for null statement, but maybe not a good solution. I cannot find a statement like python pass

{}() 

for switch case, break is better choice.

break

Tags:

Swift