在Qt中使用Swift [英] Using Swift with Qt

查看:511
本文介绍了在Qt中使用Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以很容易地在Qt中使用 Objective-C和C ++ .

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

看过苹果公司在WWDC 2015上的许多谈话,我希望Swift可以取代Objective-C进行OS X(和iOS)开发;所有的演示都使用Swift.

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.

考虑到您可以结合使用带有桥接头的Objective-C和Swift,是否可以在Qt项目中编译Swift代码并使用C ++访问Swifts第一类对象(类,结构,枚举等)?

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

如果可能的话...

从Qt调用Objective-C函数需要将代码包装在C函数中,并带有从Qt调用的C标头.

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

从Objective-C调用Swift需要桥接头,以指示可用的Swift文件.然后在XCode项目中引用此标头.我们可以在Qt .pro中做到这一点吗?如果可以,怎么办?

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?

假设我们可以指定桥接头,那么我们仍然只能从Objective-C文件中调用Swift,但是可以在C ++中直接从Qt调用Swift吗?

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

推荐答案

从Qt调用Objective-C函数需要将代码包装在C函数中,并带有从Qt调用的C标头.

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

不太正确,可以直接从Obj-C ++调用Obj-C和Obj-C ++函数和方法.鉴于Obj-C ++(主要是C ++),Qt和Obj-C/C ++之间的接口很简单.您可以简单地将Qt实现文件命名为.mm而不是.cpp!您可以直接从Obj-C方法实现中调用Qt或标准C ++,将其编译为Obj-C ++文件(.mm,而不是.m),反之亦然.

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.

有一种方法可以哄骗swift编译器为您生成桥接标头,并且可以将其作为自定义编译器或自定义目标集成到.pro文件中.

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.

这篇关于在Qt中使用Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆