I would like to run a piece of code only if the iOS version of the current device is below a specific version, as specified here. The code examples given by Apple look like this:
if (@available(iOS 10.0, *)) {
// iOS 10.0 and above
} else {
// below 10.0
}
However, there are scenarios where one would like to run code only if the current iOS version is below a specific version. I assumed the following code will work:
if (!@available(iOS 10.0, *)) {
// below 10.0
}
However it seems that this doesn't work, and I'm getting the following warning from Xcode:
@available does not guard availability here; use if (@available) instead
Here is the LLVM commit that added the diagnostic I'm seeing.
There are two possible fallbacks to that issue:
- Use the
if-else
variant without adding any code to theif
block (not very elegant). - Continue to use old approaches such as
-[NSProcessInfo isOperatingSystemAtLeastVersion:]
.
Is there another intended way to use @available
that I'm missing?
@available
with any other condition or instruction in an if. So basically the only way I can think of is having an emptyif
body but do the action within theelse
block. This seems as the only possible way to me. – Bothersome