加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift:iOS部署目标命令行标志

发布时间:2020-12-14 05:48:39 所属栏目:百科 来源:网络整理
导读:如何在Swift条件编译语句中检查iOS部署目标? 我尝试过以下方法: #if __IPHONE_OS_VERSION_MIN_REQUIRED __IPHONE_8_0 // some code here#else // other code here#endif 但是,第一个表达式导致编译错误: Expected '' or '||' expression TL; DR? 转到3.
如何在Swift条件编译语句中检查iOS部署目标?

我尝试过以下方法:

#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0
    // some code here
#else
    // other code here
#endif

但是,第一个表达式导致编译错误:

Expected '&&' or '||' expression
TL; DR? >转到3.解决方案

1.在Swift中进行预处理

根据Apple documentation on preprocessing directives:

The Swift compiler does not include a preprocessor. Instead,it takes
advantage of compile-time attributes,build configurations,and
language features to accomplish the same functionality. For this
reason,preprocessor directives are not imported in Swift.

这就是为什么在尝试使用__IPHONE_OS_VERSION_MIN_REQUIRED< __IPHONE_8_0这是一个C预处理指令.使用swift,你就不能将#if与<等运算符一起使用.你所能做的就是:

#if [build configuration]

或条件:

#if [build configuration] && ![build configuration]

2.条件编译

从the same documentation开始:

Build configurations include the literal true and false values,
command line flags,and the platform-testing functions listed in the
table below. You can specify command line flags using -D <#flag#>.

>真与假:不会帮助我们
>平台测试功能:os(iOS)或arch(arm64)>不会帮助你,搜索一下,无法确定它们的定义. (在编译器本身可能?)
>命令行标志:我们走了,这是你可以使用的唯一选择……

3.解决方案

感觉有点像解决方法,但做的工作:

现在,例如,您可以使用#if iOSVersionMinRequired7而不是__IPHONE_OS_VERSION_MIN_REQUIRED> = __IPHONE_7_0,假设您的目标当然是iOS7.

这基本上与在项目中更改iOS部署目标版本相同,只是不太方便……
当然,根据您的iOS版本目标,您可以使用相关方案进行多个构建配置.

苹果肯定会改进这一点,可能还有一些像os()这样的内置函数……

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读