为什么呢?总是崩溃,当我使用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)
}
}