0

I am working on a function related to the native part of ios written in Object-C,

I want to write a method that returns a string in Object-C,

but I am facing a problem when I write this method: Expected selector for Objective-C method,

My code is as follows:

RCT_EXPORT_METHOD((NSString *)getName)
{
  return "Sammeme";
}
@end

Anyone doing ios development can help me to solve this problem,

Thank you

3
  • What exactly is the problem you are facing? Commented Dec 22, 2021 at 8:24
  • when I write the above method it throws an exception as: Expected selector for Objective-C method , so I want to solve it. Do you know what I mean? Commented Dec 22, 2021 at 8:29
  • Is this the complete code? Is there also an @start somewhere? Is this in a class? Could you show the rest of the class? Commented Dec 22, 2021 at 8:34

1 Answer 1

1

In your Implementation file, inside of your RCT_EXPORT_METHOD, in order to return a value you can either use a promise or a callback. I like to use a promise:

RCT_EXPORT_METHOD(returnStringFromNativeModule:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject) {
    resolve(@"This is a value returned from a Native Module");
}

and in your React Native code, this is how you would call your method:

import { NativeModules } from 'react-native';


const { myNativeModule } = NativeModules;
const myString = await myNativeModule.returnStringFromNativeModule();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.