问题
I'm trying to get MIDI sessions working with swift code and am running into issues with some of the delegates.
This is a working example in Objective-C.
OSStatus status = MIDIClientCreate(CFSTR("MIDI Client"), MIDIStateChangedHandler, nil, &client);
void MIDIStateChangedHandler(const MIDINotification *message, void *refCon)
{
NSLog(@"MIDIStateChanged!");
}
This is what I'm trying with Swift:
var status = MIDIClientCreate("the client", notifyProc: MIDIStateChangedHandler, notifyRefCon: nil, outClient: client)
func MIDIStateChangedHandler(message: MIDINotification, refCon: UnsafeMutablePointer<Void>)
{
println("MIDIStateChanged!");
}
This is the error which I cannot figure out (I'm new to iOS with many years of C# experience):
cannot convert the expression's type '(StringLiteralConvertible, notifyProc: (MIDINotification, refCon: UnsafeMutablePointer) -> (), notifyRefCon: NilLiteralConvertible, outClient: @lvalue MIDIClientRef!)' to type 'StringLiteralConvertible'
var status = MIDIClientCreate("the client", notifyProc: MIDIStateChangedHandler, notifyRefCon: nil, outClient: client)
Apple does not have a Swift example for MIDINotifyProc or other MIDI functions I want to use and I can't figure out the correct method parameter type.
回答1:
Currently there is no way convert Swift functions to C functions, so it is not possible to do this only with Swift. Here an example on how to do it using Objective-C as least as possible: Objective-C Wrapper for CFunctionPointer to a Swift Closure
来源:https://stackoverflow.com/questions/26925334/how-do-i-define-a-midinotifyproc-in-swift