The recent @available addition to Objective-C can be handy but is apparently quite short-sighted..
Any app being around for more than a couple weeks will need to target e.g. multiple macOS versions.
Unfortunately the only @available construct for explicitly targeting older OS releases that appears to work (as of Xcode up to and including 12) is this cumbersome
if (@available(macOS 11.0, *))
{
// all hunky-dory: no-op
}
else
{
// legacy work arounds
...
...
}
Using just the else branch with a negation emits the dreaded @available does not guard availability here compiler warning..
Is there any way to simplify this ghastly code and remove the dummy no-op branch?
You could use the preprocessor, define a macro:
#define ifNotAvailable(A, B) if (@available(A, B)) {} else
and then write your code as:
ifNotAvailable(macOS 11.0, *)
{
// legacy work arounds
...
...
}
Whether this is less "ghastly" is in the eye of the beholder, YMMV!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With