Apple has released Xcode 9.3 beta 4. With beta 4 we have now conditional compilation in swift.
Conditional compilation in swift using canImport()
With new canImport() we can now test whether the module is available in the environment and can be imported or not. With swift going open source and many server side frameworks also coming on scene this is a welcome new update.
How to use canImport()?
canImport() takes the module name to be imported as argument and returns true if it is present.It does not import the argument.Example:
#if canImport(UIKit) <module-specific code> #endif
New platform condition targetEnvironment()
A new platform condition targetEnvironment() has been added which takes only one valid argument simulator. Conditional compilation can be used to detect whether the build environment is simulator or not. Example
#if targetEnvironment(simulator) <simulator-specific code> #endif
You can download the latest Xcode9.3 beta 4 from here