Switch must be EXHAUSTIVE? Default?

删除default语句,会出现什么错误?
  1. let vegetable = "red pepper"
  2. switch vegetable {
  3. case "celery":
  4.     let vegetableComment = "Add some raisins and make ants on a log."
  5. case "cucumber", "watercress":
  6.     let vegetableComment = "That would make a good tea sandwich."
  7. case let x where x.hasSuffix("pepper"):
  8.     let vegetableComment = "Is it a spicy /(x)?"
  9. default:
  10.     let vegetableComment = "Everything tastes good in this soup"
  11. }
  12. // error: switch must be exhaustive, consider adding a default case
在转换之前如果我们声明了vegetableComment常量,那么就无需default语句,因为它的默认值已经被声明了。

评论

此博客中的热门博文

使__attribute__((annotate("xxx")))能作用于OC函数

viewWillAppear不执行的解决办法

iOS中使用xpc/xpc.h