【问题标题】:Check Xcode/iOS SDK version used to compile the app in Swift检查用于在 Swift 中编译应用程序的 Xcode/iOS SDK 版本
【发布时间】:2019-09-25 16:56:32
【问题描述】:

假设我正在构建一个 SDK,并且我希望它能够在 Xcode 10 和 Xcode 11 上运行。 我该怎么做才能让这样的代码也能在 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 上我得到这个编译错误:

类型“UIStatusBarStyle”没有成员“darkContent”

在 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

但这在 Swift 中不起作用

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

【问题讨论】:

  • 不完美,但您可以使用#if compiler(>=5.1) ... #endif。 5.1是Xcode 11自带的编译器版本,所以Xcode 10不会编译代码。
  • 谢谢!我将选择UIStatusBarStyle.init(rawValue: 2),因为在这种情况下它使我的代码更简单,但如果你把它作为答案,我会接受它,因为这就是我所要求的。
  • 更正UIStatusBarStyle.darkContent 是UIStatusBarStyle.init(rawValue: 3),而不是2

标签: ios swift xcode


【解决方案1】:

不完美,但你可以使用

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-26
    • 2014-03-08
    • 2020-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多