Search code examples
iosswiftobjective-ccordova

Command Delegate is not working calling from Swift to Objective C


I'm trying to bridge Swift 5 and the existing Objective c Function. Typically the Cordova request hits the Swift layer which inturn calls Objective C and returns back the result to Cordova. During compilation, I'm getting this error

       NSLog(@"Result : %@ ", result);
       CDVPluginResult* pluginResult;
       pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:result];
       // Below line throwing "No known class method for selector 'commandDelegate'"
      [[self commandDelegate] sendPluginResult:pluginResult callbackId:command.callbackId];

enter image description here


Solution

  • The commandDelegate property is an instance property, not a class property. The clue is in the error "No known class method" - the code above must be currently in a class method (denoted by + in ObjC), but the commandDelegate property can only be referenced from an instance method (denoted by -). self in this context is a reference to the class itself, rather than an instance of the class.

    Make sure the enclosing function is an instance method (if that's what you need). Also make sure that your plugin is implementing the CDVPlugin interface (@interface MyPlugin : CDVPlugin in your plugin's header file), otherwise you won't be able to access the commandDelegate property.