morgancodes
morgancodes

Reputation: 25265

What does this Objective-C (or maybe c) syntax do?

I came across this in some example code:

- (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);
}

What does "logTheError:" do? What is this syntax called? Where can I go for more information on it?

Upvotes: 2

Views: 278

Answers (3)

CRD
CRD

Reputation: 53000

Its a label. Programming practice has discouraged their use for the last century or two ;-) But occasionally they are useful.

In this code sample require_noerr is a macro which takes two arguments, it tests the first and if it is not noErr does a jump (goto) to the second argument - which must be a label.

The sample code is a bit convoluted, it is equivalent to:

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);

Upvotes: 1

Abizern
Abizern

Reputation: 150565

It looks like a label to me. have a look at the source for require_noerr method in the line above.

Upvotes: 0

Carl Norum
Carl Norum

Reputation: 224844

logtheError: is a label. The require_noerr macro has a goto in it that will jump to a specified label in the case of an error. Here's a simplified and expanded goto/label example without any funny business or macros:

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

   err = function2();

 errorExit:
   return err;
 }

It's C syntax originally. You can learn more in the C standard, section 6.8.1 Labeled statements.

Upvotes: 6

Related Questions