2011-12-23 156 views
2

我碰到這在一些示例代碼來:這個Objective-C(或可能是c)語法是做什麼的?

- (IBAction) startPlayLowNote:(id)sender { 

    UInt32 noteNum = kLowNote; 
    UInt32 onVelocity = 127; 
    UInt32 noteCommand = kMIDIMessage_NoteOn << 4 | 0; 

    OSStatus result = noErr; 
    require_noerr (result = MusicDeviceMIDIEvent (self.samplerUnit, noteCommand, noteNum, onVelocity, 0), logTheError); 

logTheError: 
    if (result != noErr) NSLog (@"Unable to start playing the low note. Error code: %d '%.4s'\n", (int) result, (const char *)&result); 
} 

什麼是 「logTheError:」 怎麼辦?這個語法叫什麼?我可以在哪裏獲得更多信息?

回答

6

logtheError:是一個標籤。 require_noerr宏中有一個goto,在出現錯誤時它將跳轉到指定的標籤。這裏有一個簡化和擴展的goto/label示例,沒有任何有趣的商業或宏:

int call2Functions(void) 
{ 
    int err = function(); 
    if (err) 
    goto errorExit; 

    err = function2(); 

errorExit: 
    return err; 
} 

原來是C語法。您可以在C標準中瞭解更多信息,部分6.8.1標記語句

0

它看起來像一個標籤給我。在上面的行中查看require_noerr方法的源代碼。

1

它的一個標籤。編程實踐已經阻止了他們在上個世紀或兩個世紀的使用;-)但偶爾他們是有用的。

在此代碼示例require_noerr是宏採用兩個參數,它測試第一和如果它不是noErr做了跳躍(goto)的第二個參數 - 它必須是一個標籤。

示例代碼是有點繞口,它是等效於:

OSStatus result = MusicDeviceMIDIEvent (self.samplerUnit, noteCommand, noteNum, onVelocity, 0); 
if (result != noErr) 
    NSLog (@"Unable to start playing the low note. Error code: %d '%.4s'\n", (int) result, (const char *)&result);