精通
英语
和
开源
,
擅长
开发
与
培训
,
胸怀四海
第一信赖
锐英源精品开源,禁止转载和任何形式的非法内容使用,违者必究
I just changed my .m files to .mm and use C++. Is there a way to do the same with Swift?我刚刚将.m文件更改为.mm并使用C ++。有没有办法用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:
不。当你从.m切换到.mm时,你实际上是从Objective-C切换到另一种叫做Objective-C ++的语言(有很多细微差别)。所以你并没有真正使用C ++; 您正在使用Objective-C ++,它接受大多数C ++作为输入(与C ++接受大多数但不是所有C作为输入,这类似)。当我说它不是C ++时,考虑一个包含一个名为nil(这是合法的C ++)的变量的C ++文件,然后尝试将其编译为Objective-C ++。
SWIFT没有相同的关系。它不是C或C ++的超集,也不能直接在.swift文件中使用。
You cannot import C++ code directly into Swift. Instead, create an Objective-C or C wrapper for C++ code.
all of us with Cocoa apps incorporating C/C++ code bases now have to maintain projects written in 3 languages我们使用Cocoa C ++代码库来开发Cocoa应用程序,现在所有人必须维护用3种语言编写的项目
The confusion may come from the assumption that merely changing a file extension from .m to .mmis all you need to bridge the languages, when, in reality, it does nothing of that sort. It is not the .mmthat causes friction with .cpp, it is the .h header which must positively not be a C++ header.这种混淆可能来自这样的假设,即只需要将.m文件扩展名更改.mm,完成桥接语言所需的全部内容,而实际上它并不是那种类型。.mm它不会引起.cpp分歧,要考虑到.h头文件且必定不是C++头文件情况。
In the same project, you can happily mix C, C++, Objective-C, Objective C++, Swift, and even Assembly.
在同一个项目中,您可以愉快地混合使用C,C ++,Objective-C,Objective C ++,Swift甚至Assembly。
In the same file, you can't mix all 5. In the same source file:
在同一个文件中,你不能混合所有5项在同一个源文件中:
I wrote a simple Xcode 6 project that show how to mix C++, Objective C and Swift code:
https://github.com/romitagl/shared/tree/master/C-ObjC-Swift/Performance_Console
In particular the example call an Objective C and a C++ function from the Swift.
The key is to create a shared header Project-Bridging-Header.h and put the Objective C headers there.
Please download the project as a complete example.
我写了一个简单的Xcode 6项目,展示了如何混合使用C ++,Objective C和Swift代码:
特别是该示例从Swift调用Objective C和C ++函数。
关键是创建一个共享头Project-Bridging-Header.h并将Objective C头放在那里。
请下载该项目作为完整示例。
I have just made a little example project using Swift, Objective-C and C++. It's a demo of how to use OpenCV stitching in iOS. The OpenCV API is C++ so we can't talk to it directly from Swift. I use a small wrapper class who's implementation file is Objective-C++. The Header file is clean Objective-C, so Swift can talk to this directly. You have to take care not to indirectly import any C++-ish files into the the headers that Swift interacts with.
The project is here: https://github.com/foundry/OpenCVSwiftStitch
我刚刚使用Swift,Objective-C和C ++制作了一个示例项目。这是一个如何在iOS中使用OpenCV拼接的演示。OpenCV API是C ++,所以我们不能直接从Swift中与它调用。我使用一个小包装类,它的实现文件是Objective-C ++。该头文件是干净的Objective-C,所以SWIFT进行直接通话这一点。您必须注意不要将任何C ++ - ish文件间接导入Swift交互到的头文件中。
You can also skip the Objective-C file in between. Just add a C header file with a .cpp source file. Have only C declarations in the header file and include any C++ code in the source file. Then include the C header file in the **-Bridging-Header.h.
The following example returns a pointer to a C++ object (struct Foo) so Swift can store in a COpaquePointer instead of having struct Foo defined in the global space.
Foo.h file (seen by Swift - included in the bridging file)
您还可以跳过其间的Objective-C文件。只需添加一个带有.cpp源文件的C头文件。头文件中只有C声明,并在源文件中包含任何C ++代码。然后在** - Bridging-Header.h中包含C头文件。
下面的示例返回一个指向C ++对象(struct Foo)的指针,因此Swift可以存储在COpaquePointer中,而不是在全局空间中定义struct Foo。
Foo.h文件(由Swift看到 - 包含在桥接文件中)
#ifndef FOO_H #define FOO_H // Strictly C code here. // 'struct Foo' is opaque (the compiler has no info about it except that // it's a struct we store addresses (pointers) to it. struct Foo* foo_create(); void foo_destroy(struct Foo* foo); #endif
Inside source file Foo.cpp (not seen by Swift):
extern "C" { #include "Foo.h" } #includeusing namespace std; // C++ code is fine here. Can add methods, constructors, destructors, C++ data members, etc. struct Foo { vector data; }; struct Foo* foo_create() { return new Foo; } void foo_destroy(struct Foo* foo) { delete foo; }