14

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.

Roman Podymov
  • 4,168
  • 4
  • 30
  • 57

1 Answers1

17

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
}
spencer
  • 434
  • 4
  • 8
  • 1
    I am weakly linking a 3rd party SDK, since it doesn't support arm64 simulator, but this return true even in that case. I have the SDK path omitted from the framework search paths and I'm not linking either, but for some reason the `__has_include` still returns true. – Dávid Pásztor Jun 08 '22 at 12:04