Stored properties in Swift 2.0 cannot be marked as conditionally available to satisfy the swift 2.0 compiler

Originator:KevinRLundberg
Number:rdar://22020542 Date Originated:7/27/2015
Status:Open Resolved:
Product:Developer Tools Product Version:Xcode 7 beta 4
Classification:Other Bug Reproducible:Yes
 
Summary:
I have an iOS 7+ app that uses UISearchController on iOS 8 and UISearchDisplayController on iOS 7 by choosing the right one at runtime. I store them both in stored properties on my class, which works just fine in swift 1.2. However, in swift 2.0 I cannot declare this stored property on my class on its own since it's only available in iOS 8+, and I get an error. In addition, I cannot use @available() with this property as I get an error message stating "Stored properties cannot be marked potentially unavailable with 'introduced='". I need to hack around it by storing the search controller in a different stored property typed differently, and by accessing the different stored property in an @available(iOS 8.0, *)'d computed property that has the type I want.

Steps to Reproduce:
Open code from this gist in an xcode project that targets iOS 7: https://gist.github.com/klundberg/cb6d5205feb180c37345

Expected Results:
The first property in ViewController.swift triggering an error makes sense with the new availability checks. However I would expect the second property to work (the one named conditionallyAvailableSearchController) even though it's a stored property. 

Any usage of that property could be checked for by using #available(). In this case, the property is optional, so during initialization iOS 7 could just make the value be nil, and it could not be accessed by iOS 7 code with availability checks. 
@available stored properties could be forced to be optional by the compiler in cases where OS versions could not set them where that class is not available, or if non-optional @available stored properties are desired, they could hold a junk reference, or the compiler could allow those properties to not be initialized on the OS versions where the type is unavailable since any usage of such a value is not allowed by the compiler anyways on older versions.

Actual Results:
Neither the first or second defined properties build because the class is unavailable everywhere and I cannot mark the stored property with @available() annotations.

Version:
OS X 10.10.4
Xcode 7 beta 4

Comments

I have this same issue, but with an NSUserActivity property which needs to be retained for the lifetime of a UIViewController.

As an alternative solution, maybe one could make an extension marked @available with the field in it and methods using that field; for iOS versions where the type is not available, the field would be optional nil (as suggested) and the methods no-ops. In the case of NSUserActivity, it would make the feature fully optional and just a no-op if not available.

By fr.wielstra at Sept. 17, 2015, 11:40 a.m. (reply...)

Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!