我有一个很重要的用 C + + 编写的类库。我试图通过 Swift 中的某种桥梁来使用它们,而不是将它们重写为 Swift 代码。主要动机是 C + + 代码表示一个在多个平台上使用的核心库。实际上,我只是创建了一个基于 Swift 的用户界面,允许核心功能在 OS X 下工作。
还有其他问题,“如何从 Swift 调用 C + + 函数”这是 没有我的问题。要连接到 C + + 函数,可以使用下面的代码:
通过“ C”定义一个桥接头
#ifndef ImageReader_hpp
#define ImageReader_hpp
#ifdef __cplusplus
extern "C" {
#endif
const char *hexdump(char *filename);
const char *imageType(char *filename);
#ifdef __cplusplus
}
#endif
#endif /* ImageReader_hpp */
Swift 代码现在可以直接调用函数
let type = String.fromCString(imageType(filename))
let dump = String.fromCString(hexdump(filename))
我的问题更具体。如何在 Swift 中实例化和操作 C + + 类?我找不到任何关于这个的文章。