使用 Swift CFunctionPointer 将回调传递给 CoreMIDI API [英] Using Swift CFunctionPointer to pass a callback to CoreMIDI API

查看:17
本文介绍了使用 Swift CFunctionPointer 将回调传递给 CoreMIDI API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能目前这实际上是不可能的,这将是不幸的.我正在尝试调用 CoreMIDI API 来设置 MIDI 输入.这就是我在 Swift 中尝试做的:

It may be that this is actually not possible currently, which would be unfortunate. I'm trying to call the CoreMIDI API to set up a MIDI input. This is what I'm trying to do in Swift:

var midiClient = MIDIClientRef()
var inputPort = MIDIEndpointRef()
var status: OSStatus

func readProc(packetList: UnsafePointer<MIDIPacketList>, readProcRefCon: UnsafeMutablePointer<Void>, srcConnRefCon: UnsafeMutablePointer<Void>) -> Void {
}

status = MIDIClientCreate("MIDI client", nil, nil, &midiClient);

status = MIDIDestinationCreate(midiClient, "MIDI input", readProc, nil, &inputPort);

但我收到此错误:'(UnsafePointer, UnsafeMutablePointer, UnsafeMutablePointer) -> Void' 不可转换为 'MIDIReadProc'

But I get this error: '(UnsafePointer, UnsafeMutablePointer, UnsafeMutablePointer) -> Void' is not convertible to 'MIDIReadProc'

MIDIReadProc 的 typedef 如下:

MIDIReadProc's typedef is the following:

typealias MIDIReadProc = CFunctionPointer<((UnsafePointer<MIDIPacketList>, UnsafeMutablePointer<Void>, UnsafeMutablePointer<Void>) -> Void)>

有没有办法为我的 readProc 方法获取一个函数指针以传递给 MIDIDestinationCreate API?

Is there a way to get a function pointer for my readProc method to pass to the MIDIDestinationCreate API?

推荐答案

在 Swift 2.0(作为 Xcode 7 的一部分)中,处理函数指针的 C API 使用带有注释的函数类型 @convention(c).您可以将任何 Swift 函数、方法或闭包作为 @convention(c) 函数类型传递——但前提是该闭包符合 C 约定......它无法从其周围范围捕获状态.

In Swift 2.0 (as part of Xcode 7), C APIs that deal in function pointers use function types that are annotated @convention(c). You can pass any Swift function, method, or closure as a @convention(c) function type — but only if that closure conforms to C conventions... e.g. it can't capture state from its surrounding scope.

详情参见Swift 编程语言中的类型属性.

For details, see Type Attributes in The Swift Programming Language.

至于 Xcode 6 中的内容:Swift 1.x 无法将 Swift 函数或闭包转换为 C 函数指针——CFunctionPointer 类型的唯一用途是将从 (Obj)C API 导入的函数指针传递给其他 (Obj)C API.

As for what's in Xcode 6: Swift 1.x doesn't have a way to convert a Swift function or closure to a C function pointer -- the sole use of the CFunctionPointer type is to pass function pointers imported from (Obj)C APIs to other (Obj)C APIs.

您可以在 C 代码中声明一个函数指针,通过项目的桥接头向 Swift 公开该函数指针,然后使用 Swift 将其传递给 CoreMIDI.但既然你无论如何都要跨过一座桥,你可能会考虑项目的哪些部分最好保留在 C 中,以及从这些部分到 Swift 代码的最佳接口是什么.

You can declare a function pointer in C code that you expose to Swift via your project's bridging header, then use Swift to pass that to CoreMIDI. But since you're going to be reaching across a bridge anyway, you might instead think about which parts of your project are best to keep in C and what the best interface is from those parts to your Swift code is.

这篇关于使用 Swift CFunctionPointer 将回调传递给 CoreMIDI API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆