During the beta period of Xcode 12, we wanted to start implementing features that relied on the new SDK, but still keep our code compiling with the previous SDK. I've found myself wanting to do a compile-time check on the availability of certain SDKs.
For example, I would want to write code like this:
func doStuff() {
if #available(macOS 11.0, *) {
// Use macOS 11.0-only code
WidgetCenter.shared.reloadAllTimelines()
}
}
However, the above code wouldn't compile at all with Xcode 11, because it didn't know about WidgetCenter
at all.
I worked around it by wrapping the whole thing in an #if compiler(>=5.3)
check. This meant that when compiling with Xcode 11, that code would be skipped entirely, but on Xcode 12 it would be present. We could start working on new features by using Xcode 12, but if we compiled with Xcode 11 they would be transparently excluded from the final build product.
Unfortunately, this was conflating the Swift compiler version with the platform SDK version. The final release of Xcode 12 came out, and it does not include the macOS 11.0 SDK, so now all my code is broken again.
What I really wanted was not #if compiler(>=5.3)
, but rather #if available(macOS 11.0)
. I recognize that this could cause confusion between these two:
if #available(macOS 11.0, *) // run-time check
#if available(macOS 11.0, *) // compile-time check
So I'm happy to consider other spellings of the compile-time check. But it seems like it might be nice to have some way to check the SDK at compile-time.