如何在Swift中定义MIDINotifyProc? [英] How do I define a MIDINotifyProc in Swift?
问题描述
我正在尝试使MIDI会话使用快速代码工作,并且遇到了一些委托人的问题.
I'm trying to get MIDI sessions working with swift code and am running into issues with some of the delegates.
这是 Objective-C 中的一个有效示例.
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!");
}
这就是我正在尝试的 Swift :
var status = MIDIClientCreate("the client", notifyProc: MIDIStateChangedHandler, notifyRefCon: nil, outClient: client)
func MIDIStateChangedHandler(message: MIDINotification, refCon: UnsafeMutablePointer<Void>)
{
println("MIDIStateChanged!");
}
这是我无法弄清楚的错误(我是具有多年C#经验的iOS初学者):
This is the error which I cannot figure out (I'm new to iOS with many years of C# experience):
无法将表达式的类型'(StringLiteralConvertible,notifyProc:(MIDINotification,refCon:UnsafeMutablePointer)->(),notifyRefCon:NilLiteralConvertible,outClient:@lvalue MIDIClientRef!)'转换为'StringLiteralConvertible'
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示例,并且我无法弄清楚正确的方法参数类型.
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.
推荐答案
当前无法将Swift函数转换为C函数,因此仅使用Swift无法做到这一点.下面是一个示例,该示例说明了如何至少使用Objective-C做到这一点:
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
这篇关于如何在Swift中定义MIDINotifyProc?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!