【问题标题】:Calling callback function from c++ to objective c从c ++调用回调函数到目标c
【发布时间】:2013-06-24 00:57:23
【问题描述】:
  1. 我在目标 c 项目中使用 c++ 库。
  2. 我集成了 c++ 库并实现了 .mm 文件以桥接 c++ 和目标 c。
  3. 我可以使用这个 .mm 桥从我的目标 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. 所以现在我关心的是我应该如何以及何时在我的目标 C 库中调用这些 CALLBACK 函数,在我看来它们是在 c++ 库中内部调用的。

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

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

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

    }

  3. 我一直在研究它,并且已经努力将这个库集成到我的目标 C 库中,现在由于这些回调函数,我被困住了。 请举例说明我应该如何使用 c++ 的回调函数,包装它并在我的目标 c 中调用以获取/知道我的登录函数的结果/返回将对我很有帮助。

【问题讨论】:

    标签: c++ objective-c


    【解决方案1】:

    简单的答案是:

    1. 编写回调函数背后的逻辑是它们应该被调用 当响应来自服务器或发生某些事件时在内部进行。
    2. 如果你想使用这个,你需要实现逻辑让我们说存储 变量中的值并在此回调后返回此变量 函数被调用。 (不推荐)
    3. 如果你想在其他平台上使用这个回调函数,可以说 然后目标 C 将此回调函数与委托桥接。 (推荐)

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-02
      • 1970-01-01
      • 2015-11-01
      • 1970-01-01
      相关资源
      最近更新 更多