Swift and Objective-C++ Interoperability - c++

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.

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

Swift vs Objective-C - for reusing C++ code

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.

What is the difference between c++, objective-c and objective-c++?

I want to know the difference between c++ and objective-c and objective-c++.
Can any one give me the difference and Can we use the c++ for iPhone development
Thank you,
Madan Mohan
C++ is Bjarne Stroustroup's language based on adding classes and metaprogramming to C in such a way that puts most additional work into the compiler, and relies on least possible effort at runtime.
Objective-C is Brad Cox's language based on adding a SmallTalk-style dynamic message-passing runtime library to C, with a small amount of syntax addition to make it easier to use.
Objective-C++ is, to put it bluntly, what you get when you add the Objective-C runtime and syntax to C++. It has its limitations (e.g. you can't create an Objective-C subclass of a C++ class or vice versa, and Objective-C doesn't like C++ namespaces) but allows you to use C++ classes from Objective-C objects and vice versa.
You can use Objective-C++ in iPhone development. What this means practically is that you could write an application whose object model was entirely C++, where the controller layer would need to interface to Objective-C in order to use the Cocoa Touch APIs.
C++ and Objective C were/are two different approaches to adding object orientation to C. Current objective C compilers also accept C++ as input, so you can build a program with some files written in Objective-C and other files written in C++. When C++ is used this way, it's often called Objective-C++.
1) C++ is a language derived from C that adds Object Orientation (OO) amongst other features. *
2) Objective-C is a language derived from C that adds Object Orientation (OO) amongst other features. *
3) Objective-C++ is Objective-C that you can use C++ classes with.
You CAN use C++ for iPhone development but you will need "some" Objective-C code to interface with the iPhone libraries.
(*) Though they both try to solve the same problem they do it quite differently. There is some information about the differences on wikipedia and I'm sure you can use google to find more.
You CAN use C++ for iPhone development but you will need "some" Objective-C code to interface with the iPhone libraries.
This will very likely give you code and possibly memory bloat. As you know, iOS programming should be a lean as possible: minimize both the library size and runtime memory needs. iOS programming and runtime environments are also highly optimized for Objective C.
Pure ObjC is much better than C++ for iOS. Unless you're trying to use existing large C++ code base it will probably be better to re-write from scratch. Nearly all of the C++ STL have analogs in the iOS frameworks, often easier to use, and highly optimized by Apple.
Learn Objective C memory management, get familiar with the frameworks and go for it.

What are the limitations of C++ running on the iPhone?

I like C++ a lot and to be honest the Objective-C "super set" of C is more of a "super fail". Can an iPhone application be written in pure C++? Are there parts of the API that are unavailable from C++?
You can't code purely in C++. For one, the UIApplicationDelegate class every application needs to inherit is Objective-C.
However, nothing is stopping you from coding everything that isn't framework related in Objective-C++. You'll still need to use the Objective-C calls for UIKit and other frameworks, but all of your application logic can be in C++.
From the Objective-C++ section of the Objective-C programming guide, these are the main limitations:
Objective-C++ does not add C++
features to Objective-C classes, nor
does it add Objective-C features to
C++ classes. For example, you cannot
use Objective-C syntax to call a C++
object, you cannot add constructors or
destructors to an Objective-C object,
and you cannot use the keywords this
and self interchangeably. The class
hierarchies are separate; a C++ class
cannot inherit from an Objective-C
class, and an Objective-C class cannot
inherit from a C++ class. In addition,
multi-language exception handling is
not supported. That is, an exception
thrown in Objective-C code cannot be
caught in C++ code and, conversely, an
exception thrown in C++ code cannot be
caught in Objective-C code. For more
information on exceptions in
Objective-C, see “Exception Handling.”
Some Carbon APIs exist in the iPhone so you will be able to access them from within a purely c++ application. That being said, however, there are a lot of very important APIs that are Objective-C only (e.g., UIKit). With some good design decisions a reasonable C/C++ shim could be written to encapsulate the Objective-C necessary to get an iPhone application working. Once that's done you'd be able to write your app with traditional C++ no problem.
The way Objective-C++ is set up you can still write bona fide C++ and mix-in Objective-C calls where necessary. This has been the route I've taken in the iPhone applications I have developed.
I don't think that you'll manage to completely escape objective-c for any meaningful application, but you are free to code mostly in C++. In fact, sio2(one of the iphone game engines) is mostly C++ if you were to use that as the base for your application you could probably avoid writing objective-c yourself.
I would like to know what you don't like about objective-c? I came from a C++ background and find Obj-C to be refreshing and more OO than C++.
You only need to do the GUI in Obj-C (Obj-C++).
There is no problem with integrating any other C++ code with the interface. Just be carefully to manually delete your pointers that are held inside Obj-C objects