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