Cocoa and Objective-C++ - c++

I think I understand how Objective-C++ works in relation to Cocoa, and I am fairly versed in the basics of command-line C++ but sort of afraid to try mixing it with Objective-C. I can create a GUI with Interface Builder in Xcode, and even make classes (.h and .m files) for the interface automatically. The outlets, maybe actions (I still get those confused) leave the curly brackets open to put your own code in. So could I just put C++ code in those brackets to to take input from those buttons (and see the output, etc.)?
I guess if this is the case, one wouldn't have to learn any Obj-C, as long as they knew where to put the C++ code! Am I wrong?
Also, could anyone help me figure that out or at least point me somewhere I can learn more about this? This topic is fairly scarce on the web and I don't understand Apple's documentation.

My suggestion would be to have Objective-C files in your project (the .m file), then keep your Objective-C++ in separate files (as diciu mentions, .mm files).
One strategy would be use to these Objective-C++ classes simply as wrappers over C++ classes: write your code in C++, in .cpp files like normal. Then, when you need to access some functionality from your C++ from an Objective-C file, write a wrapper Objective-C++ class and call that from your Objective-C.
This last bit feels like a lot of work for small projects, and it is, but if you have a lot of C++ that you need to use, and a comparatively little bit of Objective-C to write, this works pretty well. (Imagine a situation where you are writing a Cocoa interface for a well-factored Windows or Linux program: where all the application logic was written in a platform agnostic manner, and you "just" have to write the UI in Cocoa to get it running on the Mac.
But, another reason for doing this wrapper approach is that Objective-C++ is a hybrid language, so has limitations as to what it's capable of, in both directions.

The thing you need to understand about objective-c++ is that the name is a lie. or at least implies a lot more than you might think.
Objective-c++ does not mean you can write hybrid objective-c++ "objects". It means that you can use c++, and objective c, in the same file. they can reference each other directly - the cannot inherit or derive from each other.

Rename .m files to .mm and you will be able to write (Objective-)C++ code in them.

Related

Developing for iOS in C++ (with/without Xcode)

What are the methods of developing for iOS with C++? I am familiar with using Objective-C++ within Xcode, by selecting Obj-C as my language when creating a new project, but what else is there. I know there are tools like Xamarin that allow for iOS development in C#, however i'm curious as to what officially supported ways of developing for iOS in C++ there are? Can an iOS app be written completely in C++ with no Objective-C and compile?
I have an app that pretty freely intermingles Objective-C (.mm files), Objective-C++ (.m files), and C++ (.cpp) as needed. The C++ code is legacy functionality that implements the heavy lifting of our app and has worked across a number of platforms for 20+ years. No need to re-implement that.
To write a C++ object in XCode, just do it. Create .cpp and .h files like you would anywhere else.
When you need to interact with a C++ object from Objective-C, you can either go into the properties of the .m file in XCode and change it to use the Objective-C++ compiler, or (better yet) just name it .mm instead of .m. Then you can include your C++ header files with standard #include statements, create C++ objects with new, delete them with delete, and access members with dot notation and pointer notation just like you would from C++.
Objective-C is a lot easier to use when dealing with the UI and system calls. It isn't hard to learn. There are probably ways to access all of those objects and methods from C++, but the gymnastics you would have to do each time might make it easier to simply learn enough Objective-C (or Swift) to do those parts in the language Apple intended. (Keep in mind that to figure out how to do everything in C++ while looking at sample code and documentation that assumes Objective-C or Swift, you're going to have to master those languages anyway.)
Yes, you can even go to extreme lengths to avoiding Objective-C, you won't be able to do so completely since UIKit depends on it, but nearly every other framework has a C API. Take a look at this question and the answer to it How to write iOS app purely in C
C is not C++, but the answer will give you a hint on what you can do. It will involve including the objc runtime, and using objc_msgsend.

Real time processing data using algorithms written in c++

Here is the scenario:
My swift app collects data from bluetooth and should process them in real-time. These data comes mainly from IMU(gyroscope, magnetometer and accelerometer).
Algorithms that process all of that is going to be written in c++ and utilize some libraries like Eigen. How should I approach such problem? From what I've found:
1) put c++ files into my project, write wrapper in objective-c and bridge it to Swift. Also now sure if I can include Eigen in mobile app easily. This would be tedious process I suppose
2) Get all algorithms as library( .dll, .lib and call it directly from swift, not sure if it's possible)
3) Rewrite all algorithms to Swift, utilize Eigen substitute for Swift, not sure if anything like this exist. Also, this solution is less efficient and would probably fail because of deadlines.
How should I approach such problem? How to solve it in a most efficient way, where I can make use of already exisiting c++ code?
I believe some of you would see this question as opinion based, but I do not know how to state this problem in a way that excludes any ambiguity.
How to run c++ files that uses Eigen in iOS app written is Swift?
Thanks in advance
You cannot call c++ from Swift. However, you can call c++ from objective-c++. And you can call objective-c++ from both Swift and objective-c.
Just make sure that your public-facing #interface code (in the .h file) contains only objective-c. (no classes, no templates, no namespaces, no including c++ headers).
Your #implementation goes in a file with extension .mm which will compile as objective-c++ - giving you an objective-c interface with the full use of c++ in the implementation of the Objective-C object.
c++ objects live quite happily as the member variables of an objective-c++ implementation. However, they will be default-constructed then the objc runtime calls Init. If you are using objects which don't have default constructors, you will need to either wrap them in a boost::optional or a std::unique_ptr (etc).
You can them import the objective-c objects into your Swift program.
Full example for anyone who has not done this before:
https://github.com/madmongo1/swift-to-cpp-demo.git

C++ algorithm in iOS application

I've got a DSP audio aglo in C# that I'd like to incorporate into iOS. A friend suggested that I convert to C++ instead of Objective-C, as it would be platform agnostic. Is this a good practice, any pitfalls to be aware of? My instinct is to keep it native with Obj-C. How would I access C++ functions within iOS?
Thank you
You can freely mix C++ with Objective-C, it's known as "Objective-C++". To let the compiler know that is your intent, you need to rename you .m file as .mm.
Mixing code like that is not necessarily a good idea, and fairly pointless if you aim is portability. Better is to keep you C++ in separate .cpp files, and create a bare-bones wrapper class (obj-c++) to pass data between your objective-C and C++ objects (effectively the wrapper is mediating obj-C methods and C++ functions to send and return data).
I have recently posted a couple of simple image processing examples of this on github - openCVSquares and openCVStitch. It's worth you taking a look, just to see how the C++ code is separated out from obj-C.
The openCV libraries have a C++ interface, so using C++ in the project is unavoidable. It would be possible to mash all of the code into .mm files, but that's an easy way to make a mess.
As regards portability, you get this by separating your UI code from your data-manipulation code. You should be doing this anyway for MVC, so it will be little additional work to write your model in C++ as per these examples. The model is portable, the UI is - and should be - platform specific.
Whether you should do this will depend on other factors such as your fluency with each language, the choice of frameworks and libraries, and the likelihood of porting. Audio processing would be a natural candidate for such an approach though.
Some detail about the samples
Don't worry if you can't get the code to run (the openCV framework can be troublesome...) the main thing is to see how the code is pieced together. I've tried to keep the examples very simple.
The C++ code was ported from C++ samples included with the openCV distro: squares.cpp and stitching.cpp. I tried to alter these as little as possible in order to assess this idea of portability.
Roughly, I had to address two issues...
1/ removed any UI code (so we are just sending data back to the obj-C side).
2/ modify the main() function so it becomes a callable function from the obj-C side. In the .cpp header file I declare a simple class with a single static function to facilitate this. Obviously there is scope to get much more elaborate but this should be enough for you to get the idea.

Objective-C++ Absolute Confusion Over UI; Best Practices

I've been working at iOS and some Mac programming for years now, and have started reading the documentation for c++. It interests me to learn how to use c++ in iPhone apps, but all that I've read assumes that you've jumped the precipice of a learning curve that comes with c++ AND ObjC. So, ill start with the basics.
Let's say I have class A, and class A wants to implement a simple function from .cpp file B. I would then Create a .mm file, so class A could mix objC and c++, but my problem is that I have no idea WHERE the c++ code goes...
In a regular objC method, I would declare methods inside the .h after the #interface line, but with c++, it's just a mess to me...
But then a harder example. What if I want to display UI elements from a C++ class on the iPhone? I have found little to no blog posts or discussions regarding c++ UI (save those lauding the QT library), and nothing related to iOS UI in c++.
Does anybody have code, or best practices for tightly integrating c++ code into an iOS app?
I think you have the wrong impression of what objective-C++ is. Objective-C++ just lets your Objective-C code work with C++ and vice versa. It doesn't bridge the two languages or object models, it only supports integration within the same source file.
For example, when using UI elements you would still need to use objective-C classes. However, those classes could then interact with C++ classes in the same implementation file.
C++ classes are usually defined in headers as well, just like Objective-C classes. Then in the source file you'd implement their methods etc.
I recommend a good C++ introduction to get familiar with C++.
What if I want to display UI elements from a C++ class on the iPhone? I have found little to no blog posts or discussions regarding c++ UI
That is because that, while possible, is simply not necessary. As you can mix ObjC and C++ together in ObjC++ source files, you can just use the usual libraries.

What Are Some Quirks/Surprises with Using .mm Files in Objective-C?

I want to use some C++ STL collections in my Objective-C iPhone app. Apparently this is possible by giving files the extension ".mm" . What are some of the quirks/surprises associated with this?
I want to use the basic containers that I'm familiar with (vector, queue, set, ...)
Cheers!
See Using C++ With Objective-C for a detailed list of what you can and can't do. You can do most things that you would expect. You just can't do things like have a C++ class inherit from an Objective-C class or vice-versa, you can't mix C++ exceptions with Objective-C exceptions, and C++ introduces several new keywords not present in Objective-C.
The major quirk of Objective-C++ is that if you don't pass -fobjc-call-cxx-cdtors to g++, it won't call the constructor and destructor of C++ instance variables in of ObjC objects. So remember to turn that option on and you should be good.
The files will be compiled with the Objective-C++ compiler, so things will behave more like g++ than gcc, but since that's what you want it probably doesn't count as a surprise. From the Objective-C side, I'm not aware of any gotchas, and I've worked on several large projects that made significant used of Objective-C++. Just keep in mind that Objective-C reserved words are reserved in Objective-C++ as well, so you can occasionally run into issues with third-party C++ libraries (e.g., if they use "id" as a parameter in a public interface).
You'd be surprised how well it works. I haven't really gotten into any problems intermixing Obj-C and C++ in a .mm file.