Home > Software design >  Stored properties with @available is not possible
Stored properties with @available is not possible

Time:09-03

I want to use a feature that is only available in >= iOS 16 therfore I use the @available flag but it does not work because "Stored properties cannot be marked potentially unavailable with '@available'"

 @available(iOS 16.0, *) @State private var photoPickerItems = [PhotosPickerItem]()

CodePudding user response:

That makes sense, and is expected. The memory map of the object’s properties gets defined at compile-time. The compiler wants all instances of your class to contain a fixed set of properties so it knows where to find the properties in memory.

If you want properties to be available or not depending on the OS version, make those properties Optional and then write instance method code that uses an @available to either load a value into each property if the OS is available, or leave it nil if not.

  • Related