如何在Swift中为交换机案例创建noop块? Swift强制您在您的案例下至少有一个可执行语句,包括默认值。我试着放一个空{},但Swift不会接受。这意味着Swift的开关情况不能完全在if-else之间翻译,反之亦然,因为在if-else你允许在条件内有空代码。
例如
switch meat { case "pork": print("pork is good") case "poulet": print("poulet is not bad") default: // I want to do nothing here }
default: break
苹果在this article谈论这个关键字。也见here。
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.