ATV
ATV

Reputation: 4516

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?

Upvotes: 3

Views: 254

Answers (1)

CRD
CRD

Reputation: 53010

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!

Upvotes: 2

Related Questions