无法使用“@available”将具有关联值的枚举案例标记为潜在不可用

Enum cases with associated values cannot be marked potentially unavailable with '@available'

我在以下代码

上收到一条错误消息 Xcode 13
@available(iOS 13.0, *)
    case windowScene(_: UIWindowScene, windowLevel: UIWindow.Level)

Enum cases with associated values cannot be marked potentially unavailable with '@available'

有谁知道我为什么会收到此错误以及纠正它的解决方案是什么?它在 Xcode 12.

中运行良好

有关 Swift 编译器团队的解释和解决方法,请参阅 this Swift bug

This is intentional. The ABI of enum cases with payloads that are potentially unavailable is not well-defined. That this worked in the past was by coincidence of your application not requiring the (potentially unavailable) type metadata for the payload. Please either increase your deployment target or mark Foo itself as available as the least-available case. https://github.com/apple/swift/pull/36327

所以您要么需要将整个枚举标记为 @available(iOS 13.0, *),要么需要将部署目标增加到 iOS 13.0

Xcode 13 Beta 3 的发行说明中列出,这是编译器中的一个错误导致了这种情况的发生。

这是节选:

The compiler used to erroneously accept @available annotations on enum cases with associated values that were newer than the deployment target. (80238318)

For example:

@available(macOS 12, *)
public struct Crayon {}

public enum Pen {
  case pencil

  @available(macOS 12, *)
  case crayon(Crayon)
}

While this worked in some cases, there was no way for the Swift runtime to perform the requisite dynamic layout needed in general, so this could cause crashes at runtime. The compiler now rejects such availability newer than the deployment target on enum cases.


因此,尽管您以前可以这样做,但现在不能这样做,因为编译器无法执行所需的检查。

或者您可以将整个枚举标记为 @available。但是,在不知道完整上下文的情况下可能很难找到解决方案。

只需更新您的 pods。我认为您使用的一些 pods 对于 Xcode 13 来说已经过时了。例如,名为 SwiftMessages 的 pod 存在此类问题。之后,pod update,这个问题就解决了。 希望对你有帮助。

我在使用 pod 'SwiftMessages' 显示弹出消息时收到了同样的消息。

我刚刚将 pod 'SwiftMessages' 替换为 pod 'SwiftMessages', '~> 5.0'.