gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 06:50:05 24 4
gpt4 key购买 nike

假设我正在构建一个 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 上我得到这个编译错误:

Type 'UIStatusBarStyle' has no member '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)
if #available(iOS 13.0, *) {
style = UIStatusBarStyle.darkContent
}
#endif.

5.1 是 Xcode 11 自带的编译器版本,所以代码不会在 Xcode 10 中编译。你仍然需要额外检查运行时 if#available编译时 #if 检查。

关于ios - 检查用于在 Swift 中编译应用程序的 Xcode/iOS SDK 版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58103249/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com