Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invert Objective-C @available?

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?

like image 607
ATV Avatar asked Apr 01 '26 21:04

ATV


1 Answers

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!

like image 85
CRD Avatar answered Apr 04 '26 11:04

CRD