16

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++?

Community
  • 1
  • 1
TheDarkKnight
  • 27,181
  • 6
  • 55
  • 85
  • It's theoretically possible with a bridging header. What have you tried already? – JAL Jun 16 '15 at 14:14
  • @JAL, but is it theoretically and practically possible? As far as I can tell, Swift only has a direct relationship to Objective-C, not C++ – TheDarkKnight Jun 16 '15 at 14:42
  • Well, you would need to create a C or Objective-C wrapper for the Qt C++ code. See: http://stackoverflow.com/questions/24042774/can-i-mix-swift-with-c-like-the-objective-c-mm-files – JAL Jun 16 '15 at 14:56
  • @JAL, that would allow calling of C++ from Swift, but for Qt we'd want it the other way round - calling Swift code from C++, so that one can, for example, create a Swift class that does something distinct such as [send a NSNotification through the notification centre](http://stackoverflow.com/questions/23404158/qt-run-object-c-code/23404257#23404257). In this example the body of MyNotification::Display would be written with Swift notation, instead of Objective-c, or just call a Swift method from a class that resides in a .swift file. – TheDarkKnight Jun 16 '15 at 16:04

1 Answers1

2

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.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
  • *"Obj-C and Obj-C++ functions and methods can be called directly from Obj-C++"* - Of-course, that's by design of Obj-C, or did you mean *"directly from C++"*? Can you point me in the direction of the method to get the swift compiler to generate the bridging header for Swift in C++? Would this will be different from the bridging header between Swift and Objective-C? – TheDarkKnight Jun 17 '15 at 08:12
  • 2
    @TheDarkKnight No, the bridging header for Obj-C++ is simply the one from Obj-C, since it is also valid Obj-C++. Since Qt's use of C++ makes it valid Obj-C++ as well, you can simply rename all your .cpp files to .mm and everything should still work. – Kuba hasn't forgotten Monica Jun 17 '15 at 22:20