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

如何在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()这样的内置函数……

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...