How do I define a MIDINotifyProc in Swift?

会有一股神秘感。 提交于 2019-12-11 07:11:00

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!