使用Swift CFunctionPointer传递一个回调到CoreMIDI API [英] Using Swift CFunctionPointer to pass a callback to CoreMIDI API

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

问题描述

这可能是目前实际上是不可能的,这将是不幸的。我试图调用CoreMIDI API来设置MIDI输入。这是我想在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'



MIDIReadProc的typedef如下:

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

有没有办法获得一个函数指针,让我的readProc方法传递给MIDIDestinationCreate API? / p>

解决方案

在Swift 2.0(目前处于测试阶段,作为Xcode 7测试版的一部分),处理函数指针的C API使用函数注释 @convention(c)的类型。你可以传递任何Swift函数,方法或闭包作为 @convention(c)函数类型 - 但只有当闭包符合C约定...。它无法从其周围范围捕获状态。



有关详细信息,请参阅类型属性

>




至于当前的测试版本... Swift 1.x没有办法转换Swift函数或闭包到C函数指针 - CFunctionPointer 类型的唯一用途是将从(Obj)C API导入的函数指针传递给其他(Obj)C API。



您可以通过项目的桥接头在C代码中声明一个函数指针,然后使用Swift将其传递给CoreMIDI。但是,由于你要穿过桥梁,无论如何你可能会考虑你的项目的哪些部分最好保持在C和什么最好的接口是从这些部分到你的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);

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

MIDIReadProc's typedef is the following:

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

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

解决方案

In Swift 2.0 (currently in beta, as part of the Xcode 7 beta), 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.

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


As for what's currently out of beta... 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.

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天全站免登陆