We have an android application having the critical code written in C++ in JNI library.
We are about to implement iOS version of the same application. The plan is to implement in Objective-c as it's straight forward to integrate C code with it. However, as Swift is picking up, we would like to have suggestion on Swift vs Objective-C keeping in mind that we have to use the existing C++ libraries and any Swift bottlenecks.
The way to reuse C++ code is to write an Objective-C wrapper class, with the header file containing nothing that is C++, but the implementation file written in Objective-C++ (.mm suffix).
An Objective-C class can be used from Swift and from Objective-C, so for a Swift project, it doesn't matter if you have a few Objective-C classes.
There is no way currently to call C++ from Swift directly, and I wouldn't expect it for a while. And if you look at how you call C from Swift, you'll probably decide that you are quite happy with the situation.
Thanks for responding. After careful consideration, we decided to go with Objective-C and even released the application.
One of the key reason was that most developers in our company are proficient in C/C++ and Objective-c was easy to get comfortable with than the Swift.
Integration with existing C/C++ code was easy without any bridging code once we defined the Makefile templates and used them within existing Makefiles. Only additional thing was to make scripts for fat libraries.
Hence based on our experience, I will recommend Objective-C over Swift.
There is no problem writing a Swift application and integrate in it a c++ source code.
Here are the steps:
1) Add the c++ source files to your Xcode project. If you are using a 3rd party libreries you will need to add it as framework to your project.
2) create a Header bridge file and add it to you project. This file will have the c++ functions signature you will like to use from the swift modules. as explained in https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html
3) When you call the c++ functions mention in the Header Bridge file you need to create a special pointers object, as described in -
https://gist.github.com/neilpa/b430d148d1c5f4ae5ddd
Hope this will help.
Related
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.
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
I have a C++ API with which I need to interact inside a Swift project. I know I cannot interact with C++ from Swift directly, but I know I can do this through an Objective-C wrapper. That much is clear.
However, I cannot find anywhere if Swift and Objective-C++ can interop with each other. I'd like to write the C++ wrapper in Objective-C++, if possible, given the API's complexity. Does anyone know if Objective-C++ and Swift can interop?
Swift and objective-c++ cannot interact. As soon as there is c++ code in the objective-c bridge header that the swift compiler use, you will get a compile error.
You need to completely hide your c++ code behind a pure c or pure objective-c API.
I don't see why they wouldn't, as Objective-C++ is fundamentally Objective-C. You might have to (you want to in any case) hide the C++ object pointers as private properties in the implementation file, but other than that there shouldn't be any issues.
Obviously you'll have to completely extract/abstract the C++ objects.
I'm somewhat moderate in my C++ knowledge but know next to nothing regarding Objective-C. I am planning (and even starting to) learn Objective-C so I can attempt to write a few iOS and even Mac OS X applications but I'm very curious about something I haven't been able to find yet.
Is it possible to write an iOS and / or a Mac OS X application entirely with C++ including the UI components? If it isn't possible to do it entirely in C++ then to what degree is it possible to use mostly C++?
I haven't seen any examples that demonstrate either of these items. I am planning on writing a few mobile applications for iOS and Android and, where possible, I'd like to contain most of my logic inside of C++ code for maximum portability. I already know I can do this but I am unsure of the degree.
Short answer, yes.
For pure C++, you should take a look at the QT framework.
Otherwise, you'll have hard time for the UI part.
But also remember that Objective-C can be mixed with C++.
That's called Objective-C++ (.mm files).
You can then write code that mix C++ and Objective-C code.
With this, you can have the UI parts in Objective-C (may be better, as it will use the native frameworks for the UI components), and the other things in C++.
If you've decided to learn Objective-C, but still want to code in C++ for some parts, I would recommend this approach, instead of pure C++.
On iOS, this is also the only way. While you can code in C++, you have to use Objective-C for the UI part.
EDIT
Here are a few links to get started with Objective-C++:
Strategies for Using C++ in Objective-C Projects
From C++ to Objective-C
CocoaDev
Look this question
I don't know about Mac OS, but in IOS applications you can use C++ in logic but you have to write user interface on Objective-C.
Is it possible to write an iOS and / or a Mac OS X application
entirely with C++ including the UI components? If it isn't possible to
do it entirely in C++ then to what degree is it possible to use mostly
C++?
I think it is possible to use pure c++ if you want to depend on some 3rd part lib, then yes.
Without that 3rd part lib you have to write all the UI using objective-c the rest you can use c++ as much as you like.
Changes required in build settings to use C++ files in iOS project
under "Apple LLVM compiler 4.2 - Language" Option
C++ Language Dialect: Compiler Default
C++ Standard Library: Compiler Default
You could use Cocos2d-x engine and write pure C++ for your iOS app, including UI (cocos has basic ui classes like buttons, scroll views, table views, etc - but you need to develop your own Controllers, and Models using various C++ libraries like Hiberlite, or raw SQLite etc)
Cocos has its own interface with iOS/MacOS (a few basic .mm objective-c classes to setup Application run cycle - like app delegate, or runloop and drawing cycle)
So you just write your user code with 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.