在Qt中使用Swift

编程入门 行业动态 更新时间:2024-10-24 22:18:08
本文介绍了在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

本文发布于:2023-10-25 22:19:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1528270.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Qt   Swift

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!