- 我在 Objective C 项目中使用 C++ 库。
- 我集成了 C++ 库并实现了 .mm 文件来桥接 C++ 和 Objective C。
我可以使用这个 .mm 桥从我的 objective-c 成功调用 c++ 函数。
问题是给定 C++ 库中的方法不返回任何内容,即 Void。 例如 void login( const char* email, const char* password);
此 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();
};
所以现在我关心的是我应该如何以及何时在我的 Objective-C 库中调用这些 CALLBACK 函数,在我看来它们是在 C++ 库内部调用的。
这是我的wrapper.mm文件,包装c++方法,需要在objective c中调用。
-(void) WrapLogin:(NSString*) 电子邮件:(NSString*) pwd{
self.wrappedModelAccessMega->登录([电子邮件 UTF8String], [pwd UTF8String]); //没有返回,因为来自 C++ 库的 Login mfunction 不返回任何内容,即 void
}
我已经研究它有一段时间了,并且已经努力将该库集成到我的 Objective-C 库中,现在由于这些回调函数,我陷入了困境。 请用一个小例子来展示我应该如何使用 C++ 的回调函数,将其包装起来并调用我的 objective-c 来获取/了解我的登录函数的结果/返回,这对我来说会非常有用。
最佳答案
简单的答案是:
- 编写回调函数背后的逻辑是它们应该被调用 当来自服务器的响应或特定事件发生时在内部。
- 如果你想使用它,你需要实现逻辑,比如存储 变量中的值并在此回调后返回该变量 函数被调用。 (不推荐)
- 如果你想在其他平台使用这个回调函数 Objective C 然后将此回调函数与委托(delegate)连接起来。 (推荐)
感谢 Jaggu 先生帮助我了解了这一点。
关于c++ - 从 C++ 到 Objective C 调用回调函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17327654/