Reputation: 53351
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. That works fine on Xcode 11, but on Xcode 10 I get this compile error:
Type 'UIStatusBarStyle' has no member 'darkContent'
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
But that doesn't work in Swift
So, is there some similar way on Swift to detect that the code is running on Xcode 10 or compiled with SDK 12?
Upvotes: 3
Views: 3140
Reputation: 4692
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.
Upvotes: 3