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.
如何在Swift中为开关机箱创建noop块?Swift强制您在您的案例下至少有一条可执行语句,包括默认语句。我试着放一个空的,但斯威夫特不会接受的。这意味着Swift的切换情况在if-else和if-else之间不能完全转换,因为if-else中允许在条件中使用空代码。
e.g.
如。
switch meat {
case "pork":
print("pork is good")
case "poulet":
print("poulet is not bad")
default:
// I want to do nothing here
}
1 个解决方案
#1
48
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.
虽然在Swift中不需要break,但是您仍然可以使用break语句来匹配和忽略特定的case,或者在该case完成执行之前跳出匹配的case。
#1
48
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.
虽然在Swift中不需要break,但是您仍然可以使用break语句来匹配和忽略特定的case,或者在该case完成执行之前跳出匹配的case。