Objective-C has an @available expression in XCode 9+ / LLVM 5+ that allows you to guard a block of code to at least a certain OS version so that it won\'t emit unguarded ava
#define SUPPRESS_AVAILABILITY_BEGIN \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wunsupported-availability-guard\"")\
_Pragma("clang diagnostic ignored \"-Wunguarded-availability-new\"")
#define SUPPRESS_AVAILABILITY_END \
_Pragma("clang diagnostic pop")
#define AVAILABLE_GUARD(platform, os, future, conditions, codeIfAvailable, codeIfUnavailable) \
SUPPRESS_AVAILABILITY_BEGIN \
if (__builtin_available(platform os, future) && conditions) {\
SUPPRESS_AVAILABILITY_END \
if (@available(platform os, future)) { \
codeIfAvailable \
} \
} \
else { \
SUPPRESS_AVAILABILITY_END \
codeIfUnavailable \
}
Usage:
AVAILABLE_GUARD(iOS, 11.0, *, true, {
printf("IS AVAILABLE");
},
{
printf("NOT AVAILABLE");
});
It works by using @available as a condition with additional optional conditions. Since you lose the ability to "guard", I suppressed the unguarded warnings but I also added an extra guard there to guard the rest of the code.. This makes it so you essentially lost nothing..
You get the guarding, you get the warnings gone and you get the extra conditions..
You could do the else-code first and store the result somehow, and then do the if-code if needed. Something like this:
/**
first make default calculations, the 'else-code'
*/
id resultOfCalculations = ... ;
if (@available(iOS 11.0, *)) {
if (some_condition) {
/**
code to run when on iOS 11+ and some_condition is true
redo calculations and overwrite object
*/
resultOfCalculations = ... ;
}
}
Then, of course, the calculation has to be done twice by the phone (if the conditions are true), but you don't have to write it twice.
May not be the most elegant solution, but if you want to keep it simple, this is an alternative.