mrwheet mrwheet - 3 months ago 55
Swift Question

MusicEventIteratorGetEventInfo in Xcode 8 beta 6

Up to Xcode 8 beta 5, I could access the event info in a MusicEventIterator, using something like:

var type: MusicEventType = 0
var stamp: MusicTimeStamp = 0
var data: UnsafePointer<()>? = nil
var size: UInt32 = 0

while(hasCurrentEvent != false) {
MusicEventIteratorGetEventInfo(iterator!, &stamp, &type,
&data, &size)
// do stuff with stamp, type, data...
}


I managed to get some help at the Apple dev site on dealing with "data", but getting data in the first place is also broken. I get a warning about using "UnsafeRawPointer" in the declaration of "data", and an error about not being able to use data as an inout argument in the call to MusicEventIteratorGetEventInfo()...
Though I know they're making things more deterministic and future-proof (i.e., for the compiler), it's incredibly frustrating to have the C-interop stuff changing every few months. My MIDI file parser code has already changed 3 times...

For reference, MusicEventIteratorGetEventInfo has the signature:

func MusicEventIteratorGetEventInfo(_ inIterator: MusicEventIterator,
_ outTimeStamp: UnsafeMutablePointer<MusicTimeStamp>,
_ outEventType: UnsafeMutablePointer<MusicEventType>,
_ outEventData: UnsafeMutablePointer<UnsafePointer<Void>>,
_ outEventDataSize: UnsafeMutablePointer<UInt32>) -> OSStatus


Any help greatly appreciated.

Answer

The latest reference of MusicEventIteratorGetEventInfo shows this:

Declaration

func MusicEventIteratorGetEventInfo(_ inIterator: MusicEventIterator, 
                                  _ outTimeStamp: UnsafeMutablePointer<MusicTimeStamp>, 
                                  _ outEventType: UnsafeMutablePointer<MusicEventType>, 
                                  _ outEventData: UnsafeMutablePointer<UnsafeRawPointer?>, 
                                  _ outEventDataSize: UnsafeMutablePointer<UInt32>) -> OSStatus

The third parameter outEventData is of type UnsafeMutablePointer<UnsafeRawPointer?>. Generally, when an API claims UnsafeMutablePointer<T>, you declare a variable of type T, and pass it as an inout argument.

So, this should work:

var type: MusicEventType = 0
var stamp: MusicTimeStamp = 0
var data: UnsafeRawPointer? = nil //<- Declare a variable of type `UnsafeRawPointer?`.
var size: UInt32 = 0

while hasCurrentEvent != false {
    MusicEventIteratorGetEventInfo(iterator!, &stamp, &type,
                                   &data, &size) //<- Pass it as an inout argument.
    // do stuff with stamp, type, data...
}