How do I create a noop block for a switch case in Swift?

Swift

Swift Problem Overview


How do I create a noop block for a switch case in Swift? Swift forces you to have at least one executable statement under your case, including default. I tried putting an empty { } but Swift won't take that. Which means that Swift's switch case is not totally translatable between if-else and vice versa because in if-else you are allowed to have empty code inside the condition.

e.g.

switch meat {

   case "pork":
     print("pork is good")

   case "poulet":
     print("poulet is not bad")

   default:
     // I want to do nothing here
}

Swift Solutions


Solution 1 - Swift

default:
  break

Apple talks about this keyword in this article. See here, too.

> Although break is not required in Swift, you can still 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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBoonView Question on Stackoverflow
Solution 1 - SwiftaleclarsonView Answer on Stackoverflow