Search code examples
swiftphotokitios14

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


I have a crash when trying to use a class that contains iOS 14 only elements on iOS 12.

The class looks like this (redacted some irrelevant stuff)

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)
}

Is my way of wrapping the iOS14 only elements incorrect ?

It crashes with this stack trace:

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 ()

The whole project can be seen here: https://github.com/eure/AssetsPicker/tree/iOS14


Solution

  • Enums don't support unavailable types

    User something else.

    Ex:

    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)
     }