2015-10-28 34 views
-1

使用C编写的SDK(Linphone),我需要将在C文件中声明的调用状态更改处理函数实现为Objective-C或在Swift环境中最好。在Objective-C中实现C回调

这里是在C声明:

// declaration 
void call_state_changed(LinphoneCore *lc, LinphoneCall *call, LinphoneCallState cstate, const char *msg); 

// typedef 
typedef void (*LinphoneCoreCallStateChangedCb)(LinphoneCore *lc, LinphoneCall *call, LinphoneCallState cstate, const char *message); 

这里包含要使其实现

typedef struct _LinphoneCoreVTable{ 
    LinphoneCoreCallStateChangedCb call_state_changed;/**<Notifies call state changes*/ 
} LinphoneCoreVTable; 

这里需要LinphoneCoreCallStateChangedCb属性的结构是我的尝试:

_vTable->call_state_changed = ^(LinphoneCore *lc, LinphoneCall *call, LinphoneCallState cstate, const char *message) 
{ 

} 

这里是错误: enter image description here

这是什么正确的语法?

谢谢!

+0

你不需要实现,因为C代码*只是*在Objective-C中运行良好? – kientux

+1

请勿将代码作为图像发布。 – dandan78

回答

2

您尝试使用,当你需要的是一个(可能static)C-功能,如下所示:

static void _linphoneCallback(LinphoneCore *lc, LinphoneCall *call, 
           LinphoneCallState cstate, const char *message) 
{ 
    // do thing 
} 

... 
_vTable->call_state_changed = _linphoneCallback; 

但是我看不出你如何传递一个Objective- C级实例回调,使得回调难以在任何语言中使用,其中包括C.

+0

我只会使用notificationcenter,我需要的只是cstate的值。有可能吗? – Hokage

+0

@Hokage是的,我会这么说。 – trojanfoe

0

这样定义 typedef void (^LinphoneCoreCallStateChangedCb)(LinphoneCore *lc, LinphoneCall *call, LinphoneCallState cstate, const char *message);

和U回调它: LinphoneCoreCallStateChangedCb callback = ^(LinphoneCore *lc, LinphoneCall *call, LinphoneCallState cstate, const char *message){};

+0

回调不是块。 – trojanfoe

+0

啊,你是对的。我在想他要纠正块语法。 – sahara108