检查用于在 Swift 中编译应用程序的 Xcode/iOS SDK 版本 [英] Check Xcode/iOS SDK version used to compile the app in Swift

查看:25
本文介绍了检查用于在 Swift 中编译应用程序的 Xcode/iOS SDK 版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我正在构建一个 SDK,并且我希望它能够在 Xcode 10 和 Xcode 11 上运行.我该怎么做才能让这样的代码也能在 Xcode 10 中编译?

Let's say I'm building a SDK and I want it to work on both Xcode 10 and Xcode 11. What can I do to make some code like this to also compile in Xcode 10?

var style = UIStatusBarStyle.default
if #available(iOS 13.0, *) {
  style = UIStatusBarStyle.darkContent       
}

由于 .darkContent 仅在 iOS 13 上可用,我认为 if #available(iOS 13.0, *) 应该足够了.这在 Xcode 11 上运行良好,但在 Xcode 10 上我收到此编译错误:

Since .darkContent is only available on iOS 13, I would have assumed that the if #available(iOS 13.0, *) should be enough. That works fine on Xcode 11, but on Xcode 10 I get this compile error:

类型 'UIStatusBarStyle' 没有成员 'darkContent'

Type 'UIStatusBarStyle' has no member 'darkContent'

在 Objective-C 中我使用过这种宏

In Objective-C I've used this kind of macros

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
// Use something that is only available on Xcode 11 and Xcode 10 doesn't understand
#endif

但这在 Swift 中不起作用

But that doesn't work in Swift

那么,Swift 上是否有类似的方法来检测代码是在 Xcode 10 上运行还是使用 SDK 12 编译?

So, is there some similar way on Swift to detect that the code is running on Xcode 10 or compiled with SDK 12?

推荐答案

不完美但你可以使用

#if compiler(>=5.1)
  if #available(iOS 13.0, *) {
    style = UIStatusBarStyle.darkContent       
  }
#endif. 

5.1 是 Xcode 11 附带的编译器版本,因此代码不会在 Xcode 10 中编译.您仍然需要额外的运行时 if #available 检查编译时#if检查.

5.1 is the version of the compiler that comes with Xcode 11, so the code won't be compiled in Xcode 10. You'll still need the run-time if #available check in addition to the compile-time #if check.

这篇关于检查用于在 Swift 中编译应用程序的 Xcode/iOS SDK 版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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