Binding MIDI Outputs

This took quite awhile to figure out. At first I tried and failed to use a mutable dictionary, then gave up and went with a mutable array of OutputPort.

class OutputPort: NSObject {
	let source: NSNumber
	let name: NSString
	
	init(source: NSNumber, name: NSString) {
		self.source = source
		self.name = name
		super.init()
	}
}

In this simple app the delegate is in charge of the MIDI output ports:

class AppDelegate: NSObject, NSApplicationDelegate {

	@IBOutlet weak var window: NSWindow!

	func applicationDidFinishLaunching(aNotification: NSNotification) {

		let n =  MIDIGetNumberOfDestinations()
		for i in 0..?
			MIDIObjectGetStringProperty(src,
				kMIDIPropertyName,
				&name)
			let displayName = name!.takeRetainedValue() as NSString
			Swift.print("\(displayName)")
			
			let op = OutputPort(source: NSNumber(unsignedInt: src),
				                name: displayName)
			outputPorts.append(op)
			
		}
	}

	func applicationWillTerminate(aNotification: NSNotification) {
		// Insert code here to tear down your application
	}

	dynamic var outputPorts = [OutputPort]()
}

The Output port array is the content for the Array Controller (added to the xib graphically):
Array Controller Bindings

The UI, laid out in xcode, is like so:
MIDI Outputs UI

Both the table view and the popup list the ports, and both are bound to the array controller. Here are the bindings for the popup:

Popup Bindings

It’s very easy to forget to set the Object Class for the Array Controller:

Array Controller Object Class

And there is the spectacular running application:

Running App

Leave a Reply

Your email address will not be published. Required fields are marked *