I am working on swift program to access C++ library which will need a callback function to send data back to caller asynchronizely.
On my project, I used the bridging-Header.h to access C++ files, wrap.h and wrap.cpp
In the wrap.h,
I declared a callback function
typedef void (*callbackfunctionS)();
In the wrap.cpp,
bool RegisterCallBack(callbackfunctionS ptrfn)
{
(*ptrfn)();
return false;
}
In the main.swift,
func DataCallback()->Void {
println("DataCalback called")
}
let p = UnsafeMutablePointer<()->Void>.alloc(1) // allocate memory for function
p.initialize(PenDataS) /initialize with value
let cp = COpaquePointer(p) //convert UnsafeMutablePointer to COpaquePointer
let fp = CFunctionPointer<()->Void>(cp) //convert COpaquePointer to CFunctionPointer
RegisterCallBack(fp) //Error EXC_BAD_ACCESS(code =2, address = 0x10052fa80)
I don't know how to make the C++ calling Swift function to work, Can anyone to help?
I am using Swift 2.0 on Xcode 6, new to Mac world