iosobjective-cswiftreact-nativereact-native-native-module

method is not a recognised objective c method


Lot of question are asked regarding the same but none of them solve my error.

Here is my objective c file

#import <Foundation/Foundation.h>
#import <React/RCTBridgeModule.h>
#import <UIKit/UIKit.h>

@interface RCT_EXTERN_MODULE(LanguageTranslationModule, NSObject)


RCT_EXTERN_METHOD(callbackMethod:(NSString*)englishText (RCTResponseSenderBlock)callback)

@end

Here is my swift class

@objc(LanguageTranslationModule)
class LanguageTranslationModule: NSObject {

  var resultCallback: RCTResponseSenderBlock!

  @objc func callbackMethod(_ englishText: String, callback: @escaping RCTResponseSenderBlock) -> Void {
    resultCallback = callback
    debugPrint("Hi there")
    translateText(msg: englishText)
  }...

Here is my JS call from React Native

LanguageTranslationModule.callbackMethod(englishText, (err, r) => {
        if (!err) {
          setProgress(false);
          setMarathiText(r.text.toString());
        } else {
          setProgress(false);
          setMarathiText(err);
        }
      });

Following is my translate text method

func translateText(msg: String) {

    let options = TranslatorOptions(sourceLanguage: .en, targetLanguage: .mr)
    let englishMarathiTranslator = NaturalLanguage.naturalLanguage().translator(options: options)

    let conditions = ModelDownloadConditions(
      allowsCellularAccess: false,
      allowsBackgroundDownloading: true
    )

    englishMarathiTranslator.downloadModelIfNeeded(with: conditions) {error in
      guard error == nil else { return }
      englishMarathiTranslator.translate(msg) { (translatedText, error) in
        guard error == nil, let translatedText = translatedText else { return }
        let resultsDict = [
          "text" : translatedText
        ];
        self.resultCallback([NSNull() ,resultsDict])
      }
    }
  }

have added underscore to my first parameter in swift file as that is most of the solution to other questions asked on stack as well as there is space between the underscore and actual variable name. If I remove the englishText variable from all the files and hardcode that text in swift file then my function works fine. of course then I had to add underscore to the callback variable, so no logical error from my side


Solution

  • I assume that the error is in the title of the question. It seems that your Swift method's signature doesn't match the signature declared in the Objective C interface.

    Try adding an argument label to the second parameter in the declaration.

    #import <Foundation/Foundation.h>
    #import <React/RCTBridgeModule.h>
    #import <UIKit/UIKit.h>
    
    @interface RCT_EXTERN_MODULE(LanguageTranslationModule, NSObject)
    
    
    RCT_EXTERN_METHOD(callbackMethod:(NSString*)englishText callback:(RCTResponseSenderBlock)callback)
    //                                                      ^^^^^^^^
    
    @end
    

    Explanation:

    RCT_EXTERN_METHOD(callbackMethod:(NSString*)englishText callback:(RCTResponseSenderBlock)callback)
    

    matches

    @objc func callbackMethod(_ englishText: String, callback: @escaping RCTResponseSenderBlock) -> Void
    

    while your original variant

    RCT_EXTERN_METHOD(callbackMethod:(NSString*)englishText (RCTResponseSenderBlock)callback)
    

    would match

    @objc func callbackMethod(_ englishText: String, _ callback: @escaping RCTResponseSenderBlock) -> Void