Swift CoreMIDI Callbacks

The following was tested with Swift 2.1.1.  I am far from a Swift expert.  I do use “;” and “()” in Swift sometimes even though not necessary.  This is because I develop in many languages and the code flows from my fingers this way.

Suppose you have a Cocoa app with a ViewController and you want to update the UI when a MIDI source or destination is added or removed, or a MIDI packet is received. There are a few snags along the way that you may encounter. CoreMIDI provides references for the application to pass in that can be used in the callbacks. But converting between Swift and C structs may not be intuitively obvious. Another difficulty is casting MIDINotification to MIDIObjectAddRemoveNotification. If this isn’t done properly the bits will get converted but the actual data in the MIDIObjectAddRemoveNotification struct will be trash. This is how I handled callbacks in Swift.

First I created a protocol that my ViewController implemented so that casting would be a little cleaner.

Then I implemented the callbacks as global Swift functions. The bridgeMutable() and unbridgeMutable() utility functions were based on information I found on stackoverflow.com

Finally the view controller was implemented using the following code snippets. Note that another utility function invokeLater is used so that the UI can be updated off of the high priority CoreMIDI event thread.