Consider the developing of a game: you're writing your scene under the magic and wonderful C++ world but you really need to display the Game Center leaderboard (or the Google Play Games leaderboard): it's the time to call a native Objective C method (or Java method, in case of the Android platform). So, How to do that ?
In the Objective C world
One of the thing that I really appreciate of this platform is the simplicity of the integration with C and C++ code. It's really simple, without, for example, any JNI *ucking lines of code.
What you only need to do is:
- Create a new Objective C class file through xcode (or manually, if you want. Xcode will create two files: the header file (.h) and the implementation file (.m)
- Rename the extension of the implementation file from .m to .mm; now you're telling Xcode that the .mm source file could contains C and C++ code
- Now you can use these files to contain C and C++ code like the example below, and call the function/method wherever you want
using namespace std;
static void doStuff(void);
// Objective C code inside a C++ function
NSArray *myarray = @[@"i", @"am", @"a", @"nerd"];
NSLog("My array: %@", [myarray description]);
As you have seen, I've declared the doStuff function to be static. That's a choice because. It could be non-static as well.
In the next article of this series I'll talk about how to do the same thing with Android. Stay tuned.