在 Swift 中传递 C 函数回调 [英] Pass C function callback in Swift

查看:30
本文介绍了在 Swift 中传递 C 函数回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Swift 中传递 C 回调?考虑这个例子:

How do you pass a C callback in Swift? Consider this example:

class AudioQueue {

    var desc : AudioStreamBasicDescription
    var queue : AudioQueue?

    func audioQueueHandleBuffer(ctx : UnsafeMutablePointer<()>,
                                inAQ : AudioQueue!,
                                inBuffer : AudioQueueBufferRef) {
       // do stuff
    }

    func initialize() {
        // this does not work!
        var err = AudioQueueNewOutput(&desc, audioQueueHandleBuffer,
                                      nil, nil, nil, 0, queue)

        // ...
    }
}

推荐答案

使用 Swift 2.0 现在可以在纯 Swift 中设置回调!请检查 http://oleb.net/blog/2015/06/c-callbacks-in-swift/Swift 2 无法使用类型为 的参数列表调用FSEventStreamCreate"以获取灵感:)

With Swift 2.0 it is now possible to set up the callback in pure Swift! Please check http://oleb.net/blog/2015/06/c-callbacks-in-swift/ and Swift 2 Cannot invoke 'FSEventStreamCreate' with an argument list of type for inspiration :)

这篇关于在 Swift 中传递 C 函数回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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