Obj-c.h 函数原型(prototype)中需要 C++ 结构

标签 c++ objective-c

我已经编写了一个 obj-c 包装器来连接 Swift 和 C++。方法之一

- (bool) getParamInfo:(int) paramId parameter:(Parameter *) info;

需要 C++ 文件中的参数结构。

我尝试在 obj-c header 中声明 struct Parameter 但由于 expected a type 错误而无法编译。

我无法将 C++ 文件中的参数结构移动到 obj-c.mm 文件中。

如何解决这个问题,以便 objective-c header 可以编译并公开包装器中的方法?

最佳答案

您可以通过包含包含Parameter struct

的C++ Header 来实现

Parameter.hpp 中: C++ 头文件

typedef struct{
int paramId;
} Parameter;

Param.hh 中:Objective-C++ header

#import "Parameter.hpp"
- (bool) getParamInfo:(int) paramId parameter:(Parameter *) info;

Swift-Bridging-Header.h 中:

#import "Param.hh"
#import "Parameter.hpp"

在 Swift 文件中

var pa = Parameter(paramId: 0) /* Create a struct */
let f = Param().getInfo(0, parameter: &pa)

关于Obj-c.h 函数原型(prototype)中需要 C++ 结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37897318/

相关文章:

c++ - 在 C++ 中生成缺少一些零的新 guid 字符串

c++ - DirectX 11 - 子弹调试抽奖

objective-c - 对可能被objective-c中的另一个线程锁定的对象执行 "dirty read"的最佳方法

ios - UITableViewCells 中的灰色效果

objective-c - 在 objective-c 中迭代列表并从中删除项目?

c++ - 绕行并使用 _thiscall 作为 Hook (GCC 调用约定)

c++ - CPU 数量的增加会降低性能,CPU 负载不变并且没有通信

c++ - 冒泡排序算法不起作用

ios - 在具有透明 View 的 UITableview 中显示图像列表

ios - App Delegate 的托管对象上下文为零