我可以将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屋!