转换 C++ Objective C 语法
我的背景经验是C/C++/C#。
我在 xcode 项目中使用 C++ 库(具体来说,该库是 PJSIP)。要使用该库,我必须将几个回调连接到我的代码,如下所示:
SipTest.m
#include < pjsua-lib/pjsua.h >
static void on_reg_state(pjsua_acc_id acc_id)
{
// Do work
}
static void Init()
{
// pjsua_config and psjua_config_default are defined in the header file from pjsip
pjsua_config cfg;
psjua_config_default(&cfg);
cfg.cb.on_regstate = &on_reg_state;
}
我想将此 C++ sytnax 切换到 Objective C,
所以我这样做了:
+(void) on_reg_state:(pjsua_acc_id) acc_id
{
// Do work
}
+(void) Init
{
pjsua_config cfg;
psjua_config_default(&cfg);
cfg.cb.on_regstate = &on_reg_state; // ***** this is causing compile error
// I tried [CLASS NAME on_reg_state] and i get runtime error
}
我尝试在 Objective C 中搜索委托,但找不到 a类似的情况是,回调已经在 C++ 中实现,并且您希望将其与 Objective-C 语法一起使用。
谢谢
My background experience is C/C++/C#.
I am using a C++ library in an xcode project (to be specific the library is PJSIP). To use the library i have to wire couple of callbacks to my code like this:
SipTest.m
#include < pjsua-lib/pjsua.h >
static void on_reg_state(pjsua_acc_id acc_id)
{
// Do work
}
static void Init()
{
// pjsua_config and psjua_config_default are defined in the header file from pjsip
pjsua_config cfg;
psjua_config_default(&cfg);
cfg.cb.on_regstate = &on_reg_state;
}
I want to switch this C++ sytnax to Objective C
so I did:
+(void) on_reg_state:(pjsua_acc_id) acc_id
{
// Do work
}
+(void) Init
{
pjsua_config cfg;
psjua_config_default(&cfg);
cfg.cb.on_regstate = &on_reg_state; // ***** this is causing compile error
// I tried [CLASS NAME on_reg_state] and i get runtime error
}
I tried to search for delegate in Objective C but i could not find an a similar case where the callback is already implemented in C++ and you want to use it with Objective-C syntax.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(2)
烛影斜2024-11-23 03:11:32
在这种情况下,您需要使用选择器。
+(void) on_reg_state:(pjsua_acc_id) acc_id
{
// Do work
}
+(void) Init
{
pjsua_config cfg;
psjua_config_default(&cfg);
cfg.cb.on_regstate_selector = @selector(on_reg_state:);
cfg.cb.target = self; //Self here is the class object in your 'Init' method, which is poorly named.
//Use this like [cfg.cb.target performSelector:cfg.cb.on_regstate_selector withObject:...etc]
}
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
首先,根本不需要转换任何东西。从 Objective-C 调用 C++ 库是完全可以的。
其次,导致编译器错误的原因是您试图将方法粘贴到应该有函数指针的位置。您无法使用
&
运算符从 Objective-C 方法中创建函数指针。只需保留您的on_reg_state()
函数并像以前一样使用它,这也是您在 Apple 基于 C 的框架中执行回调的方式(一旦您超出了高级功能,您就需要它)级 Objective-C API 提供)。第三,您的
+ (void)Init
方法似乎有点奇怪。我强烈建议您编写一个名为Init
(大写)的方法。如果你打算编写一个初始化程序,它应该是- (id)init
,即小写并返回id
。并且不要忘记调用其超类的指定初始化程序,检查其返回值,将其分配给 self,并在init
方法末尾返回它(请参阅实现初始化程序< /em> 在 Apple 的文档(如果您不熟悉的话)。如果您的方法不是初始化程序,请使用不同的名称,例如- (void)createConfig
。First of all, there's absolutely no need to convert anything at all. It is perfectly fine to call C++ libraries from Objective-C.
Secondly, whats causing the compiler error is that you're trying to stick a method in a place where there should be a function pointer. You can't make a function pointer out of an Objective-C method using the
&
Operator. Simply keep youron_reg_state()
function and use it as you did before, that's how you do callbacks in Apple's C-based frameworks, too (which you'll need as soon as you move beyond what the high-level Objective-C APIs provide).And thirdly, your
+ (void)Init
method seems a bit strange. I would strongly discourage you to write a method calledInit
(capitalized). If you intend to write an initializer, it should be- (id)init
, i.e. lowercase and returningid
. And don't forget to call the designated initializer of its superclass, check its return value, assign it to self, and return it at the end of theinit
method (see Implementing an Initializer in Apple's documentation if you're not familiar with that). And if your method is not an initializer, use a different name, e.g.- (void)createConfig
.