使用@convention(c)来处理Swift 2中的c回调函数

时间:2015-12-07 14:09:07

标签: swift2

为什么呢?总是崩溃,当我使用unsafeBitCast函数时,请参考以下代码和错误消息:

代码:"让obj:MyController = unsafeBitCast(context,MyController.self)"

Error: Thread11: EXEC_BAD_ACCESS(code=1, address=0x20)

public class MyController: NSObject {

    public override init() {
        super.init()
        registerEventHandler()
    }

    let callback: @convention(c) (Int ,UnsafeMutablePointer<Void>) -> Void = { event, context in

        //Always crash, when i deal with context using unsafeBitCast
        let obj: MyController = unsafeBitCast(context, MyController.self)
    }

    public func registerEventHandler() {
        let selfObj = unsafeBitCast(self, UnsafeMutablePointer<Void>.self)

        //call C function
        RegisterEventHandler(callback, selfObj)
    }
}

0 个答案:

没有答案