簡體   English   中英

從Objective-C ++轉換為Objective-C

[英]Translate to Objective-C from Objective-C++

我在.mm中有此代碼

void MyMIDINotifyProc(const MIDINotification *message, void *refCon);

這條線

MIDIClientCreate((CFStringRef)@"MidiMonitor MIDI Client", MyMIDINotifyProc, self, &client);

然后從CoreMIDI調用MyMIDINotifyProc。

在不將文件擴展名更改為.m的情況下,我將其翻譯為Obj-C簽名:

@interface MidiInput (Private) 
    -(void) MyMIDINotifyProc:(const MIDINotification *)message reference:(void *)refCon;
@end

編譯,但是后來我不知道該如何處理MIDIClientCreate行。 "MyMIDINotifyProc" was not declared in this scope ,它會"MyMIDINotifyProc" was not declared in this scope產生"MyMIDINotifyProc" was not declared in this scope 它應該是void * ...類型的文檔,並且100%清楚地知道這是A refCon passed back to notifyRefConlink )。

僅在Obj-C中可以解決此問題嗎? 我想我必須將C ++內容轉換為C,在這種情況下,我將不理會它。

MIDIClientCreate需要一個普通的C函數作為其回調。 您不能將它傳遞給Objective-C方法,因為調用約定是不同的(用C術語來說,Objective-C方法的前兩個參數是self_cmd選擇器-對於普通的C函數是不存在的)。

解決此問題的通常方法是創建一個C函數,該函數充當填充程序並顯式將self作為參數傳遞。 然后,Shim函數立即發送Objective-C消息。 我對該庫不熟悉,但是看起來refCon是專門為此類事情設計的參數。 因此,在您的.m文件中,您需要這樣的功能。

static void MyMIDINotifyProc(const MIDINotification *message, void *refCon)
{
    [(id) refCon MIDINotify: message];
}

您還需要一種方法來做實際的事情:

-(void) MIDINotify: (const MIDINotification*) message
{
    // do stuff
}

您可以完全像最初一樣調用MIDIClientCreate。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM