我可以混合Swift与C ++吗?像Objective-C .mm文件 [英] Can I mix Swift with C++? Like the Objective - C .mm files
问题描述
我只是将我的.m文件更改为.mm并使用C ++。有什么方法可以用Swift做同样的事吗?
不。当你从.m切换到.mm时,你实际上从Objective-C切换到另一种称为Objective-C ++的语言(它有许多细微的差别)。所以你不是真的使用C ++;你使用接受大多数C ++作为输入的Objective-C ++(与C ++接受大多数但不是所有C作为输入的方式相同)。当我说它不是C ++时,考虑一个包含一个名为 nil
(这是合法的C ++)的变量的C ++文件,然后尝试将其编译为Objective-C ++。 p>
Swift没有相同的关系。它不是C或C ++的超集,并且不能在 .swift
文件中直接使用。
将Swift与Cocoa和Objective-C结合使用也告诉我们:
您不能将C ++代码直接导入到Swift中。而是为C ++代码创建一个Objective-C或C包装器。
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屋!