FR: Please use @available for checking if features exist on mac

Please use @available for checking if features exist on mac. I would like to enable -Wpartial-availability to catch the use of Objective-C functions that don’t exist on all macOS versions that I deploy on.

However, JUCE uses the old school way to check if features exist, so it throws warnings when I compile. Please use @available which has been available since Xcode 9.

And then make the following change:

             if ([window respondsToSelector: @selector (setRestorable:)])
                 [window setRestorable: NO];
 
-           #if defined (MAC_OS_X_VERSION_10_13) && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_13)
-            if ([window respondsToSelector: @selector (setTabbingMode:)])
-                [window setTabbingMode: NSWindowTabbingModeDisallowed];
-           #endif
-
+            if (@available(macOS 10.12, *)) {
+                [window setTabbingMode: NSWindowTabbingModeDisallowed];
+            }
+                       

Being hit by this right now, it’s the sole reason why we can’t enable “treat warnings as errors” in certain projects

A several new warnings due to this have appeared in the accessibility code after updating JUCE to 6.1.0 and 6.1.1.

It’s easy to reproduce:

  1. Create a default plug-in project with Projucer
  2. Set the deployment target to 10.9 for all targets
  3. set “Unguarded availability” to “Yes (All versions)” at Project level as shown in the screenshot below

image

When building, this will raise 20+ warnings, with 10 or more located in juce_mac_Accessibility.mm alone.

Setting “Unguarded availability” to “Yes (All Versions)” is the only safe choice when targeting macOS 10.9, as explained here:

CLANG_WARN_UNGUARDED_AVAILABILITY build setting. If set to YES it causes the -Wunguarded-availability-new switch to be passed to the compiler.

However, this setting only checks for the use of post-10.13 APIs. If your deployment is set to 10.9 and you use a 10.11 feature, you still don’t get a warning. This is the default setting, and is why I wasn’t gettting any warnings.

To get a warning about the use of any post-deployment API feature you have to set CLANG_WARN_UNGUARDED_AVAILABILITY=YES_AGGRESSIVE (or “Yes (All Versions)” as it appears in the Xcode build settings). That setting invokes the more thorough -Wunguarded-availability compiler switch.

source: the accepted answer here: Checking compatibility with older … | Apple Developer Forums

2 Likes

I’ve silenced the new warnings in the accessibility code here:

Replacing the preprocessor version checks with @available is a larger task and something we are planning on doing now that the keyword is supported everywhere since we bumped the minimum supported Xcode version to 9.2.

4 Likes