gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:50:06 28 4
gpt4 key购买 nike

  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->Login([email UTF8String], [pwd UTF8String]); //没有返回,因为来自 C++ 库的登录函数不返回任何内容,即 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com