如何在Swift中有选择地导入框架? [英] How do you selectively import a framework in Swift?
问题描述
我有一个需要iOS和tvOS使用的共享框架,但是我想选择性地仅导入iOS框架(CoreTelephony).快速的语法说您可以添加一个属性,但这是行不通的:
I have a shared framework that needs to be used by iOS and tvOS, but I want to selectively import a framework for iOS only (CoreTelephony). The swift grammar says you can prepend an attribute, but this doesn't work:
@available(iOS 10.0, *) import CoreTelephony
@available(iOS 10.0, *) import CoreTelephony
这根本不被支持吗?我是否需要子类化才能导入iOS特定框架?
Is this simply not supported? Do I need to subclass just to import the iOS specific framework?
推荐答案
对于Swift< = 4.0,您可以使用os()
配置测试功能:
For Swift <= 4.0 you can use the os()
configuration test function:
#if os(iOS)
import CoreTelephony
#endif
您还必须包装使用CoreTelephony
的代码.
You'll have to wrap code that uses CoreTelephony
as well.
os()
的所有可用测试是:macOS
,iOS
,watchOS
,tvOS
,Linux
,Windows
和FreeBSD
.
All available tests for os()
are: macOS
, iOS
, watchOS
, tvOS
, Linux
, Windows
, and FreeBSD
.
对于Swift> = 4.1,您还可以使用 canImport()
:
For Swift >= 4.1 you can also use canImport()
:
#if canImport(CoreTelephony)
import CoreTelephony
#endif
这篇关于如何在Swift中有选择地导入框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!