Best practice for checking if an enum exists in iOS version?

CoreBluetooth has refactored both their CBCentralManager and CBPeripheralManager to inherit from CBManager.

In this new class, the CBManagerState enumeration exists. This is kind of a big change.


What is the best strategy for supporting both CBManagerState along side with CBCentralManagerState and CBPeripheralManagerState enums?


This code begins to look quite messy. I don't know of a way to check enum availability, so I check the class that this new property is used in instead.


- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
    if ([CBManager class]) {
        self.updatedStateBlock((CBManagerState)central.state);
    }
    else {
        self.updatedStateBlock((CBCentralManagerState)central.state);
    }
}


Previously my updatedStateBlock was defined as such, but now the named ns_enumeration has changed its name.

@property (nonatomic, copy) void (^updatedStateBlock)(CBCentralManagerState state);


I could possibly refactor my framework API to pass the manager instead, and then the caller would have to query the state property, and do a class availability check as above.


I understand the desire to put all of the XPC connection stuff in a super class, but I feel this change is an API breaking one.


Anyone got any slick ideas? To unify the signature I could typecast as NSInteger, but then I lose the strongly typed value and the protection in code it has.

Just replace all your CBCentralManagerState and CBPeripheralManagerState enums by CBManagerState.

The enums are binary compatible so your code will run fine on any iOS version.

If you want to still be able to compile your code with Xcode 7, you can add some really simple defines.


#if __IPHONE_OS_VERSION_MAX_ALLOWED <= __IPHONE_9_3
#define CBManagerState CBCentralManagerState
#define CBManagerStateUnknown CBCentralManagerStateUnknown
#define CBManagerStateResetting CBCentralManagerStateResetting
#define CBManagerStateUnsupported CBCentralManagerStateUnsupported
#define CBManagerStateUnauthorized CBCentralManagerStateUnauthorized
#define CBManagerStatePoweredOff CBCentralManagerStatePoweredOff
#define CBManagerStatePoweredOn CBCentralManagerStatePoweredOn
#endif

XCode does throw an error for 'CBManagerState' is only available in iOS 10.0 or newer

The code I posted was using the define __IPHONE_10_0. This define exists in Xcode 7 in QuartzCore/CABase.h but not in Availability.h.

If you project did not use the QuartzCore framework, you would have builds problems.


I updated the code to use

#if __IPHONE_OS_VERSION_MAX_ALLOWED <= __IPHONE_9_3

instead of

#if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_10_0


This should be safe unless Apple releases Xcode 7.4 with the __IPHONE_9_4 define.

Another solution would be to ensure that __IPHONE_10_0 exists with the following:


#ifndef __IPHONE_10_0

# define __IPHONE_10_0 100000

#endif

Best practice for checking if an enum exists in iOS version?
 
 
Q