简体   繁体   English

在 Swift 中检查用于编译应用程序的 Xcode/iOS SDK 版本

[英]Check Xcode/iOS SDK version used to compile the app in Swift

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? 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       
}

Since .darkContent is only available on iOS 13, I would have assumed that the if #available(iOS 13.0, *) should be enough.由于.darkContent仅在 iOS 13 上可用,我会假设if #available(iOS 13.0, *)就足够了。 That works fine on Xcode 11, but on Xcode 10 I get this compile error:这在 Xcode 11 上工作正常,但在 Xcode 10 上我得到这个编译错误:

Type 'UIStatusBarStyle' has no member 'darkContent'类型“UIStatusBarStyle”没有成员“darkContent”

In Objective-C I've used this kind of macros在 Objective-C 我用过这种宏

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

But that doesn't work in Swift但这在 Swift 中不起作用

So, is there some similar way on Swift to detect that the code is running on Xcode 10 or compiled with SDK 12?那么,在 Swift 上是否有类似的方法来检测代码是否在 Xcode 10 上运行或使用 SDK 12 编译?

Not perfect but you could use不完美,但你可以使用

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

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. 5.1 是 Xcode 11 附带的编译器版本,因此代码不会在 Xcode 10 中编译。除了编译时#if检查之外,您仍然需要运行时if #available检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM