swift - 在 swift 5 枚举 : How to suppress "Default will never be executed" warning? 中使用@unknown 默认值

标签 swift enums warnings swift-playground swift5

假设我有如下现有代码:

enum SomeEnumCases {
  case existing
  case alreadyExisting
}

func doSomething(withEnums enumCase: SomeEnumCases) {
  switch enumCase {
  case .existing:
    print("This case was already existing")
  case .alreadyExisting:
    print("This case was already existing too...")
  }
}

现在,如果我要在枚举中添加一个新的 case,上面的函数将显示一个编译错误,指出 switch case 必须是详尽无遗的,我将被迫处理新的缺失 case。我会在 switch 语句中添加第三种情况,或者添加 default 语句。

现在,为了处理这种不可预见的枚举情况,我想在上面的现有函数中添加一个 @unknown default 情况。唯一的问题是,现在它会给我一个警告说 Default will never be executed

所以问题是,我如何让我的枚举面向 future ,以便我可以:

  1. 彻底处理所有当前的枚举案例,并且
  2. 对 future 的未知情况有一个默认的处理机制,并且
  3. 只有在添加新案例时才会看到警告,并且这些案例必须由默认案例处理。

这意味着,下面的代码不应该给出警告:

enum SomeEnumCases {
  case existing
  case alreadyExisting
}

func doSomething(withEnums enumCase: SomeEnumCases) {
  switch enumCase {
  case .existing:
    print("This case was already existing")
  case .alreadyExisting:
    print("This case was already existing too...")
  @unknown default: // <-- warning: Default will never be executed: should be suppressed
    print("Alright, this is something new and exciting !!")
  }
}

但是下面的代码应该给出一个警告:

enum SomeEnumCases {
  case existing
  case alreadyExisting
  case new
}

func doSomething(withEnums enumCase: SomeEnumCases) {
  switch enumCase { // <-- warning: Switch must be exhaustive: This should stay.
  case .existing:
    print("This case was already existing")
  case .alreadyExisting:
    print("This case was already existing too...")
  @unknown default:
    print("Alright, this is something new and exciting !!")
  }
}

是否可以通过@unknown 或其他方式实现?

最佳答案

警告可能有点误导,因为 spec说(强调):

A nonfrozen enumeration is a special kind of enumeration that may gain new enumeration cases in the future—even after you compile and ship an app. Switching over a nonfrozen enumeration requires extra consideration. When a library’s authors mark an enumeration as nonfrozen, they reserve the right to add new enumeration cases, and any code that interacts with that enumeration must be able to handle those future cases without being recompiled. Only the standard library, Swift overlays for Apple frameworks, and C and Objective-C code can declare nonfrozen enumerations. Enumerations you declare in Swift can’t be nonfrozen.

与其说分支永远不会被执行,不如说该功能完全不受您的 SomeEnumCases 用户定义的 Swift 枚举的支持。

在 Swift 5 中似乎没有支持的方式来做你想做的事情,一些迹象表明添加案例被视为一个重大变化,因为它可能/会破坏二进制兼容性,但 Swift 是一个不断变化的目标......

关于swift - 在 swift 5 枚举 : How to suppress "Default will never be executed" warning? 中使用@unknown 默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55433505/

相关文章:

c++ - 使用枚举指定的函数填充 vector

javascript - 为什么我的 React 组件图标显示为 typeof 对象?

ios - 在 Hapijs 后端处理 Alamofire 在 Swift 中发送的多部分/表单数据请求

swift - 我的 Swift 代码出现错误无法形成上限 < 下限的范围

c - 如何保护枚举赋值

c - 初始化由 C 中的枚举索引的字符串数组

python-3.x - Matplotlib - 设置纵横比时的轴碰撞警告

php - "Notice: Undefined variable"、 "Notice: Undefined index"、 "Warning: Undefined array key"和 "Notice: Undefined offset"使用 PHP

swift - 如何在一对多关系的 NSSet 上应用 NSSortdiscriptor 或 NSPredicate

ios - 后台任务中的 BGAppRefreshTask