[swift-evolution] Idea: Support #if os(Darwin) as shorthand for os(iOS) || os(OSX) || os(watchOS) || os(tvOS)
Evan Maloney
emaloney at gilt.com
Mon Feb 22 12:57:25 CST 2016
>> • Is the target a "common UIKit platform" (compiles on both iOS and tvOS) or not? (Answers the question "can I import UIKit"?)
>
> Maybe test for the presence of a particular module?
>
> #if supports(Foundation)
> #if supports(UIKit)
>
> Or allow conditional importing of a module?
>
> #if import UIKit
> // Only compiled if UIKit imported; all UIKit APIs usable here
> #elseif import AppKit
> // Only compiled if AppKit imported; all AppKit APIs usable here
> #endif
> // No AppKit or UIKit APIs usable here
Being able to test for the importability of a given module/framework at runtime would be extremely helpful.
We use several frameworks that are only available in a subset of the platforms we support, and on only certain OS versions.
To work around this problem now, we dynamically load frameworks from Obj-C only when we're running on an OS version we know is supported by the framework(s) in question.
We can't dynamically load them from Swift because if they're included in an import, the runtime tries to load it right away, leading to a crash on any unsupported platform.
The only way to selectively load dynamic frameworks at runtime is to do it via Obj-C. Some sort of check like the ones you propose should let us avoid this.
More information about the swift-evolution
mailing list