3

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

5
  • Don't you need to call callback inside your swift method? Your js method never triggered if you don't call. Commented Apr 28, 2020 at 5:01
  • @ibrahimyilmaz I have called it. look in the swift code Commented Apr 28, 2020 at 10:58
  • resultCallback = callback here you just assign it to another variable. Where have you called? Commented Apr 28, 2020 at 12:12
  • You need to call like callback([NSNull(), [ "success" : true ]]). Commented Apr 28, 2020 at 12:49
  • @ibrahimyilmaz I have called that code inside translateText method which is called inside callbackMethod. Check my code again Commented Apr 28, 2020 at 15:52

1 Answer 1

4
+50

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 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.