Swift's canImport analogue in Objective-C

2.3k Views Asked by At

Swift 4.2 has a special condition canImport that helps developers to check whether a module can be imported in project. It was introduced in Swift 4.1.

Now I am working on iOS project written in Objective-C. I use modules, and for each target these modules are different. That's why I want to use something like that:

#if canImport(SomeModule)
@import SomeModule;
#endif

How can I solve this problem? Now I use different "Other C Flags" for each target, but I want to find more flexible solution.

1

There are 1 best solutions below

1
On BEST ANSWER

This is a little late as an answer, but i came across this issue while working on a similar case. I used the __has_include(<SomeModule/SomeModule.h>)

Importing your framework:

#if __has_include(<SomeModule/SomeModule.h>)
#import <SomeModule/SomeModule.h>
#define __HAS_SOME_MODULE_FRAMEWORK__
#endif

Later in your code :

- (void)doSomething {
    #ifdef __HAS_SOME_MODULE_FRAMEWORK__
    // with  SomeModule framework
    #else
    // without  SomeModule framework
    #endif
}