I would like to know if there is any preprocessor command in Swift to check if a module exists. I use CocoaPods dependency manager tool, and I would like to check during compile time so I can use it or I have to do a custom workaround.
It would be something like this:
#if module(SwiftyJSON)
#else
#endif
A proposal (SE-0075) to introduce a canImport
directive has been accepted, but it hasn't been implemented yet as of Swift 3.0.2/Swift 3.1 beta. The bug tracking the implementation is SR-1560.
With the directive, you would be able to write something like this:
#if canImport(UIKit)
// UIKit-based code
#elseif canImport(Cocoa)
// OSX code
#else
// Workaround/text, whatever
#endif
PS: "preprocessor command" is not the correct terminology for Swift because Swift doesn't have a preprocessor.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With