我正在尝试让 MIDI session 与 swift 代码一起工作,但我遇到了一些代表的问题。
这是 Objective-C 中的一个工作示例。
OSStatus status = MIDIClientCreate(CFSTR("MIDI Client"), MIDIStateChangedHandler, nil, &client);
void MIDIStateChangedHandler(const MIDINotification *message, void *refCon)
{
NSLog(@"MIDIStateChanged!");
}
这就是我在 Swift 中的尝试:
var status = MIDIClientCreate("the client", notifyProc: MIDIStateChangedHandler, notifyRefCon: nil, outClient: client)
func MIDIStateChangedHandler(message: MIDINotification, refCon: UnsafeMutablePointer<Void>)
{
println("MIDIStateChanged!");
}
这是我无法弄清楚的错误(我是 iOS 新手,有多年的 C# 经验):
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 没有 MIDINotifyProc 或我想使用的其他 MIDI 函数的 Swift 示例,我无法找出正确的方法参数类型。
最佳答案
目前无法将 Swift 函数转换为 C 函数,因此仅使用 Swift 无法做到这一点。这里有一个关于如何尽可能少地使用 Objective-C 来做到这一点的例子:Objective-C Wrapper for CFunctionPointer to a Swift Closure
关于ios - 如何在 Swift 中定义 MIDINotifyProc?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26925334/