c++ - 从 C++ 到 Objective C 调用回调函数

标签 c++ objective-c

  1. 我在 Objective C 项目中使用 C++ 库。
  2. 我集成了 C++ 库并实现了 .mm 文件来桥接 C++ 和 Objective C。
  3. 我可以使用这个 .mm 桥从我的 objective-c 成功调用 c++ 函数。

  4. 问题是给定 C++ 库中的方法不返回任何内容,即 Void。 例如 void login( const char* email, const char* password);

  5. 此 C++ 库实现了回调函数来了解此登录方法的结果。

示例:

class DemoApp : public XClass
{

int urandomfd;

public:
uint32_t dstime(void);

FileAccess* newfile();

void request_error(MegaClient*, error);

void login_result(MegaClient*, error);

void users_updated(MegaClient*, User**, int);
void nodes_updated(MegaClient*, Node**, int);

int prepare_download(MegaClient*, Node*);

void share_result(MegaClient*, int, error);

void account_details(MegaClient*, AccountDetails*, int, int, int, int, int, int);

void topen_result(MegaClient*, int, error);
void topen_result(MegaClient*, int, string*, const char*, int);

void transfer_update(MegaClient*, int, off_t, off_t, uint32_t);
void transfer_error(MegaClient*, int, int, int);
void transfer_failed(MegaClient*, int, error);
void transfer_failed(MegaClient*, int, string&, error);
void transfer_limit(MegaClient*, int);
void transfer_complete(MegaClient*, int, chunkmac_map*, const char*);
void transfer_complete(MegaClient*, int, const byte*, const byte*, SymmCipher*);
void changepw_result(MegaClient*, error);

void reload(MegaClient*, const char*);

void notify_retry(MegaClient*, int);
void debug_log(MegaClient*, const char*);

DemoApp();
};
  1. 所以现在我关心的是我应该如何以及何时在我的 Objective-C 库中调用这些 CALLBACK 函数,在我看来它们是在 C++ 库内部调用的。

  2. 这是我的wrapper.mm文件,包装c++方法,需要在objective c中调用。

    -(void) WrapLogin:(NSString*) 电子邮件:(NSString*) pwd{

    self.wrappedModelAccessMega->登录([电子邮件 UTF8String], [pwd UTF8String]); //没有返回,因为来自 C++ 库的 Login mfunction 不返回任何内容,即 void

    }

  3. 我已经研究它有一段时间了,并且已经努力将该库集成到我的 Objective-C 库中,现在由于这些回调函数,我陷入了困境。 请用一个小例子来展示我应该如何使用 C++ 的回调函数,将其包装起来并调用我的 objective-c 来获取/了解我的登录函数的结果/返回,这对我来说会非常有用。

最佳答案

简单的答案是:

  1. 编写回调函数背后的逻辑是它们应该被调用 当来自服务器的响应或特定事件发生时在内部。
  2. 如果你想使用它,你需要实现逻辑,比如存储 变量中的值并在此回调后返回该变量 函数被调用。 (不推荐)
  3. 如果你想在其他平台使用这个回调函数 Objective C 然后将此回调函数与委托(delegate)连接起来。 (推荐)

感谢 Jaggu 先生帮助我了解了这一点。

关于c++ - 从 C++ 到 Objective C 调用回调函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17327654/

相关文章:

c++ - 了解原子变量和操作

c++ - 变量或字段 'name of var'声明为void

c++ - Boost 程序选项 - 从函数结果传递 arg 名称

C++ 赋值运算符和重载

objective-c - 在日历上添加条目时调试我的iPad App崩溃

C++错误: default template arguments may not be used in function template

c++ - 从 Objective-c 委托(delegate)给 c (或者更好地称之为 : Creating listener from C to OBJ-C)

iphone - 如何将货币字符串格式化为浮点值?

objective-c - 确定给定日期的小时数(加上或减去 DST 轮类小时数)

iphone - IBAction中跳过了用户界面命令