Bypassing Swift's "availability"

HI all,


Does anyone know if there is a way to bypass Swift's "availability" in version 2?


We have a code that uses injection to add implementation for API that is unavailable in older versions of iOS, but is available in newer versions of iOS, which makes it easy to support older versions of iOS in a new API syntax.

With the introduction of the "availability" compiler errors we can't compile our Swift 2 code, the compiler doesn't know that these methods are injected at runtime and throws an error, and we can't call these methods from new API only because we need to support older versions.

Writing a wrapper is also not an option, reverting the code back to Obj C will be far less work, but this is an option we really don't want to resort to.

Does anyone have any thoughts on how to resolve this issue?


Thanks,

Uri.

Accepted Answer

Have you tried just adding the declaration of the methods to your Obj-C bridging header?


Just as a quick test, I added

@interface NSString (Compatibility)
- (BOOL)localizedStandardContainsString:(NSString *)str;
@end

to the bridging header for an app with a deployment target of 10.10, and called it from Swift.


The app built without error. Then I commented it out, and the build failed with a error of "only available in 10.11 or newer".


So it seems to work, at least for methods.

Awesome!

This worked, thank you very much !

Bypassing Swift's "availability"
 
 
Q