Crash 的类型元数据补全函数 |访问具有某些不可用组件的 class 时崩溃

Type metadata completion function for Crash | Crash when accessing a class that has some unavailable components

我在尝试使用 class 时遇到了崩溃,其中 iOS 仅包含 iOS 14 个元素 iOS 12.

class 看起来像这样(编辑了一些不相关的东西)

public class AssetFuture {
    public enum AssetRepresentation {
        case asset(asset: PHAsset)
        @available(iOS 14, *)
        case result(result: PHPickerResult)
    }
    @available(*, deprecated, message: "Use assetRepresentation instead")
    public var asset: PHAsset! {
        switch assetRepresentation {
        case .asset(asset: let asset):
            return asset
        default:
            return nil
        }
    }
    public let assetRepresentation: AssetRepresentation

    init(asset: PHAsset) {
        self.assetRepresentation = .asset(asset: asset)
    }

    @available(iOS 14, *)
    init(pickerResult: PHPickerResult) {
        self.assetRepresentation = .result(result: pickerResult)
}

我包装 iOS14 个唯一元素的方式不正确吗?

它因以下堆栈跟踪而崩溃:

Thread 1 Queue : com.apple.main-thread (serial)
#0  0x0000000100520748 in __abort_with_payload ()
#1  0x000000010051fcf8 in abort_with_payload_wrapper_internal ()
#2  0x000000010051fd2c in abort_with_payload ()
#3  0x00000001004dcb40 in dyld::halt(char const*) ()
#4  0x00000001004dcc6c in dyld::fastBindLazySymbol(ImageLoader**, unsigned long) ()
#5  0x00000001f126f708 in _dyld_fast_stub_entry(void*, long) ()
#6  0x00000001f126e210 in dyld_stub_binder ()
#7  0x0000000100637654 in type metadata completion function for AssetFuture.AssetRepresentation ()

整个项目可以在这里看到:https://github.com/eure/AssetsPicker/tree/iOS14

枚举不支持不可用的类型

用户其他东西。

例如:

public enum AssetRepresentation {
     public struct PHPickerResultWrapper {
         private let result: Any
         @available(iOS 14, *)
         public var value: PHPickerResult {
             return result as! PHPickerResult
         }
         @available(iOS 14, *)
         fileprivate init(_ result: PHPickerResult) {
             self.result = result
         }
     }
     case asset(asset: PHAsset)
     @available(iOS 14, *)
     case result(object: PHPickerResultWrapper)
 }