我可以将 Swift 与 C++ 混合使用吗?像 Objective-C .mm 文件 [英] Can I mix Swift with C++? Like the Objective-C .mm files
问题描述
我刚刚将我的 .m 文件更改为 .mm 并使用 C++.有没有办法用 Swift 做同样的事情?
I just changed my .m files to .mm and use C++. Is there a way to do the same with Swift?
推荐答案
没有.当您从 .m 切换到 .mm 时,您实际上是从 Objective-C 切换到一种称为 Objective-C++ 的不同语言(有许多细微的差异).所以你并没有真正使用 C++;您使用的 Objective-C++ 接受大多数 C++ 作为输入(与 C++ 接受大多数但不是所有 C 作为输入的方式相同).当我说它不完全是 C++ 时,请考虑一个包含名为 nil
的变量(这是合法的 C++)的 C++ 文件,然后尝试将其编译为 Objective-C++.
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 没有相同的关系.它不是 C 或 C++ 的超集,您不能直接在 .swift
文件中使用.
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.
将 Swift 与 Cocoa 和 Objective-C 结合使用" 还告诉我们:
您不能将 C++ 代码直接导入 Swift.相反,为 C++ 代码创建一个 Objective-C 或 C 包装器.
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屋!