Swift:iOS部署目标命令行标志 [英] Swift: iOS Deployment Target Command Line Flag

查看:275
本文介绍了Swift:iOS部署目标命令行标志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Swift条件编译语句中检查iOS部署目标?

How do I check the iOS deployment target in a Swift conditional compilation statement?

我尝试过以下操作:

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

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

But, the first expression causes the compile error:

Expected '&&' or '||' expression


推荐答案

TL ; DR? >转到 3。解决方案

TL;DR? > Go to 3. Solution

根据有关预处理指令的Apple文档


Swift编译器不包含预处理器。相反,它需要
的编译时属性,构建配置和
语言功能来完成相同的功能。对于这个
的原因,预处理器指令不会在Swift中导入。

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 < 等运算符。你所能做的就是:

That is why you have an error when trying to use __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0 which is a C preprocessing directive. With swift you just can't use #if with operators such as <. All you can do is:

#if [build configuration]

或条件:

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



2。条件编译



再次来自相同的文档


构建配置包括文字真值和假值,
命令行标志以及下面
表中列出的平台测试函数。您可以使用-D< #flag#>指定命令行标志。

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#>.




  • true false :不会帮助我们

  • 平台测试功能 os(iOS) arch(arm64)>不会帮助你,搜索了一下,无法确定它们的定义。 (在编译器本身可能?)

  • 命令行标志:我们走了,这是唯一可以使用的选项...

    • true and false: Won't help us
    • platform-testing functions: os(iOS) or arch(arm64) > won't help you, searched a bit, can't figure where they are defined. (in compiler itself maybe?)
    • command line flags: Here we go, that's the only option left that you can use...
    • 感觉有点像解决方法,但做的工作是:

      Feels a bit like a workaround, but does the job:

      现在您可以使用 #if iOSVersionMinRequired7 代替 __ IPHONE_OS_VERSION_MIN_REQUIRED> = __IPHONE_7_0 ,假设您的目标当然是iOS7。

      Now for example, you can use #if iOSVersionMinRequired7 instead of __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_7_0, assuming, of course that your target is iOS7.

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

      That basically is the same than changing your iOS deployment target version in your project, just less convenient... Of course you can to Multiple Build configurations with related schemes depending on your iOS versions targets.

      Apple肯定会改进这一点,可能还有一些内置函数,比如 os() ...

      Apple will surely improve this, maybe with some built in function like os()...

      这篇关于Swift:iOS部署目标命令行标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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