Nat! bio photo

Nat!

Senior Mull

Twitter Github Twitch

"Interesting" Example Code

From an AudioCore code sample:

static void        BuildDeviceMenu(AudioDeviceList *devlist, NSPopUpButton *menu, AudioDeviceID initSel)
{
   [menu removeAllItems];
   
   AudioDeviceList::DeviceList &thelist = devlist->GetList();
   int index = 0;
   for (AudioDeviceList::DeviceList::iterator i = thelist.begin(); i != thelist.end(); ++i, ++index) {
      while([menu itemWithTitle:[NSString stringWithCString: (*i).mName]] != nil) {
         strcat((*i).mName, " ");
      }
      
      if([menu itemWithTitle:[NSString stringWithCString: (*i).mName]] == nil) {
         [menu insertItemWithTitle: [NSString stringWithCString: (*i).mName] atIndex:index];
         
         if (initSel == (*i).mID)
            [menu selectItemAtIndex: index];
      }
   }
}

Also notice the clever use of if there to ensure nothing can go wrong.