Zit*_*ong 6 objective-c ios swift
在objective-c中,我有类似的东西:
#if __has_include(<MyFramework/SomeFeature.h>)
SomeFeature *h = [SomeFeature new]
#else
OtherFeature *h = [OtherFeature new]
#endif
Run Code Online (Sandbox Code Playgroud)
我们如何检查一个类是否存在于swift中?这个链接有一些答案弱链接 - 检查一个类是否存在并使用该类
好处__has_include是它是一个编译时检查,但它只适用于objective-c标头.我们有什么适合快速的吗?或者也许我正在做的不是一个好方法?
例如,我有一个类Machine,它有一个方法Gun,如果我已经包含一个Pistol框架,它将返回Pistol.如果我既包括Pistol和AK47,它将返回AK47,因为它有更多的子弹.
小智 5
您可以在 Swift 4 中使用#if canImport(ModuleName)。
#if canImport(UIkit)
import UIKit
#elseif canImport(MyBus)
import MyBus
#else
// Workaround/text, whatever
#endif
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
974 次 |
| 最近记录: |