Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check for NS_AVAILABLE_IOS with const CGFloat

When a method can only be used on certain iOS versions, I usually check for its availability using respondsToSelector:.

With a const CGFloat declaration this is not possible.

The specific constant I'm trying to use is UIFontWeightBlack which is defined as:

UIKIT_EXTERN const CGFloat UIFontWeightBlack NS_AVAILABLE_IOS(8_2);

What is the best way to check if the iOS version my code is running in supports this constant?


Also if I want to support building my framework with older versions of the iOS SDK, what is the best way to check at compile time, if the used SDK offers this symbol?

I would currently do the check with

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_2

Is there a way to check for the symbol directly, without having to rely on / explicitly specify the iOS SDK version?

like image 568
Tim Bodeit Avatar asked Dec 24 '22 12:12

Tim Bodeit


1 Answers

This is covered in the SDK Compatibility Guide.

Check the availability of an external (extern) constant or a notification name by explicitly comparing its address—and not the symbol’s bare name—to NULL or nil.

So your code would be:

if (&UIFontWeightBlack != NULL) {
    // It is safe to use the constant
}
like image 61
rmaddy Avatar answered Dec 28 '22 08:12

rmaddy