Swift's canImport analogue in Objective-C

时间:2018-10-02 09:08:47

标签: objective-c module preprocessor-directive swift4.1

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.

2 个答案:

答案 0 :(得分:4)

这是一个较晚的答案,但是我在处理类似案件时遇到了这个问题。 我使用了__has_include(<SomeModule/SomeModule.h>)

导入您的框架:

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

稍后输入代码:

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

答案 1 :(得分:0)

@import SomeModule;

在代码中,您使用if ([ClassFromSomeModule class])

检查模块是否存在