Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Swift with Qt

Tags:

c++

macos

swift

qt

We can, quite easily, use Objective-C with C++ in Qt.

Having watched many of Apple's WWDC 2015 talks, I expect that Swift will supersede Objective-C for OS X (and iOS) development; all the demonstrations used Swift.

Considering that you can use Objective-C and Swift together, with a bridging header, is it possible to compile Swift code in a Qt project and access Swifts first class objects (Classes, Structs, Enums etc) with C++?


If it is possible...

Calling an Objective-C function from Qt requires wrapping the code in a C function, with a C header to be called from Qt.

Calling Swift from Objective-C requires a bridging header to denote which Swift files are available. This header is then referenced in an XCode project; can we do this in a Qt .pro and if so, how?

Assuming we can specify the bridging header, we've still only made it possible to call Swift from the Objective-C files, but can Swift be called directly from Qt, in C++?

like image 403
TheDarkKnight Avatar asked Jun 16 '15 13:06

TheDarkKnight


1 Answers

Calling an Objective-C function from Qt requires wrapping the code in a C function, with a C header to be called from Qt.

That's not quite true, Obj-C and Obj-C++ functions and methods can be called directly from Obj-C++. Given that Obj-C++ is (mostly) C++, the interfacing between Qt and Obj-C/C++ is trivial. You can simply name your Qt implementation files .mm instead of .cpp! You can call Qt or standard C++ directly from Obj-C method implementations, compiled as Obj-C++ files (.mm, not .m), and vice-versa.

There's a way to coax the swift compiler to generate a bridging header for you, and this could be integrated into the .pro file as a custom compiler or a custom target.

like image 109
Kuba hasn't forgotten Monica Avatar answered Sep 28 '22 04:09

Kuba hasn't forgotten Monica