#if canImport(module) 仍然无法解决 Swift 4.1 中的条件导入语句? [英] #if canImport(module) still does not solve conditional import statement in Swift 4.1?

查看:34
本文介绍了#if canImport(module) 仍然无法解决 Swift 4.1 中的条件导入语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用支持 iOS 9 版本.我添加 Icecream 框架来同步领域对象的框架之一需要 iOS 10 或更高版本.

My App supports from version iOS 9. One of the frameworks that I am adding Icecream framework to sync realm objects needs iOS 10 or greater.

如何使框架的导入有条件?

How to make the import of the framework conditional?

我尝试在项目常规标签下的链接框架和库下将 IceCream 框架设为可选

I have tried making the IceCream framework as optional under Linked frameworks and libraries under projects General tab

在 Swift 4.1 中,您可以使用 canImport 指令进行条件导入,就像我在下面尝试过的那样.

In Swift 4.1 you can have conditional import using canImport directive, like the one below which I have tried.

#if canImport(IceCream)
  import IceCream
#endif

上面的import语句还是抛出构建错误:Modules deployment target is iOS10

The above import statement still throws a build error: Modules deployment target is iOS10

我缺少什么配置?

推荐答案

您需要将 IceCream 框架的调用功能也包含在该宏中,例如

you need to include the called functionality of your IceCream framework also with that macro like

#if canImport(IceCream)
  let iceCream = IceCream()
  let text = ceCream.toString()
#else
  // and now?
#endif

你应该考虑 else 代码.

And you should think about the else code.

这篇关于#if canImport(module) 仍然无法解决 Swift 4.1 中的条件导入语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆