如何在Swift中为交换机案例创建noop块?

前端之家收集整理的这篇文章主要介绍了如何在Swift中为交换机案例创建noop块?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何在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.

原文链接:https://www.f2er.com/swift/320902.html

猜你在找的Swift相关文章