我可以将 Swift 与 C++ 混合使用吗?像 Objective-C 的 .mm 文件 [英] Can I mix Swift with C++? Like the Objective-C .mm files

查看:25
本文介绍了我可以将 Swift 与 C++ 混合使用吗?像 Objective-C 的 .mm 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I just changed my .m files to .mm and use C++. Is there a way to do the same with Swift?

解决方案

No. When you switch from .m to .mm you are actually switching from Objective-C to a different language (which has many subtle differences) called Objective-C++. So you're not really using C++; you're using Objective-C++ which accepts most C++ as input (in the same way that C++ accepts most but not all C as input). When I say it's not quite C++, consider a C++ file that includes a variable named nil (which is legal C++) and then try to compile that as Objective-C++.

Swift doesn't have the same relationship. It is not a superset of C or C++, and you can't directly use either in a .swift file.

"Using Swift with Cocoa and Objective-C" also tells us:

You cannot import C++ code directly into Swift. Instead, create an Objective-C or C wrapper for C++ code.

这篇关于我可以将 Swift 与 C++ 混合使用吗?像 Objective-C 的 .mm 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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