1

i have problem with calling some function in c++ from objective-c. I have the following code. In "objCFuncA" how do i call "funcBCallback(std::string inText)" function?. Thanks

SampleCallback.h

class SampleCallback
{
public:
    static void funcA();
    static void funcBCallback(std::string inText);
};

SampleCallback.mm

void SampleCallback::funcA()
{
    [[SampleCallback_iOS shared] objCFuncA:"My text from C++"];
}

void SampleCallback::funcBCallback(std::string inText)
{
    NSLog(@"SUCCES GET RETURN");
}

SampleCallback_iOS.h

@interface SampleCallback_iOS : NSObject

+ (SampleCallback_iOS*)shared;

- (void)objCFuncA:(const char*)inString;

@end

SampleCallback_iOS.m

@implementation SampleCallback_iOS

static SampleCallback_iOS* instance = nil;

+ (SampleCallback_iOS*)shared
{
    @synchronized(self) {
        if (instance == nil) {
            instance = [[self alloc] init];
        }
    }
    return instance;
}

- (void)objCFuncA:(const char*)inString
{
    NSLog(@"Text from C++: %s", inString);

    // How to call SampleCallback::funcBCallback?
}
@end
1
  • 2
    You have to rename the SampleCallback_iOS.m to .mm and use #include"SampleCallback.h", After that is just as simple as SampleCallback::funcBCallback("your text"); Commented Apr 20, 2015 at 12:04

1 Answer 1

1

It's not possible to call C++ functions from plain Objective-C (like you can't call C++ from plain C).

You can convert your Objective-C file to Objective-C++, though. To do so, just rename the file to use the extension .mm instead of .m.

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.