I am having some trouble calling an objective-c class method from a C++ file. Example:
In my .h:
@interface MyClass : NSObject {
}
+ (void)myMethod:(NSString *)str;
In my .m:
+ (void) myMethod:(NSString *)str { ... }
In my .cpp:
??
How can you call myMethod since there is no class instance? Its essentially a static?
Thanks
Objects in C++ are incompatible with objects in Objective-C. You cannot simply call an Objective-C method from C++.
There are some solutions, however:
Use Objective-C++. Rename your
.cppto.mm, then you can use Objective-C syntax in your C++ code:[FlurryAnalytics myMethod: @"foo"];Use direct calls to the Objective-C runtime system. I won’t tell you how to, because I really think you shouldn’t, and in fact that you don’t want to.
Write a plain-C interface. That is, in some
.mfile, definevoid myFunction(const char *str) { ... }and call that from C++. You can find an example of this here.