0

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

Antzi
  • 12,831
  • 7
  • 48
  • 74
  • I don't have iOS12, but on iOS 13 works fine, so if the issue would be in iOS14 availability then it would rise on iOS 13 as well, I assume. Maybe the reason is somewhere else? Maybe iOS 13 availability also? – Asperi Aug 28 '20 at 12:16
  • Hi Asperi, I updated the code on git but can’t post an answer yet. Thank you so much for helping me. I feel like this should be dealt with by the compiler but I have a workaround. – Antzi Aug 28 '20 at 14:31

1 Answers1

0

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)
 }
Antzi
  • 12,831
  • 7
  • 48
  • 74