Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check at compile time if a Swift module exists on the current project?

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
like image 540
KabraBoja Avatar asked Feb 15 '17 14:02

KabraBoja


1 Answers

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.

like image 108
Ole Begemann Avatar answered Oct 17 '22 01:10

Ole Begemann