I am trying to access an Objective C singleton from Swift, however I only seem to get the initial value created in the init function of the singleton. The flightControllerState object exposed is updated in a delegate function and I can see that the value is properly updated on the Objective C side.
I have followed a few different posts here on SO and also this article on how to call the shared object from Swift. (I should also mention this is running inside a react native project if that may have any impact?)
Objective-C Singleton
@import DJISDK;@interface RCTBridgeDJIFlightController : RCTEventEmitter<DJIFlightControllerDelegate> { DJIFlightControllerState *flightControllerState;}@property(nonatomic, readonly) DJIFlightControllerState *flightControllerState;+ (id)sharedFlightController;@end@implementation RCTBridgeDJIFlightControllerDJIFlightControllerState *flightControllerState;@synthesize flightControllerState;+ (id)sharedFlightController { static RCTBridgeDJIFlightController *sharedFlightControllerInstance = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ sharedFlightControllerInstance = [[self alloc] init]; }); return sharedFlightControllerInstance; } - (id)init { // I also tried this to make sure the shared instance was returned but no luck //if (sharedFlightControllerInstance != nil) { // return sharedFlightControllerInstance; //} if (self = [super init]) { flightControllerState = nil; } return self; } -(void)flightController:(DJIFlightController *)fc didUpdateState:(DJIFlightControllerState *)state { flightControllerState = state; }@end
Swift class calling singleton and accessing values
class VirtualStickController { var flightControllerSharedInstance: RCTBridgeDJIFlightController override init() { self.flightControllerSharedInstance = RCTBridgeDJIFlightController() } func getFlightControllerState() { if let state = flightControllerSharedInstance.flightControllerState { print("FLIGHT CONTROLLER STATE: \(state)") // always null } else { print ("NULL") } }