I tried do something in a cpp class and when doing the function return callback to swift.
So i do these things :
Creating this function in
callbackClass.cpp
int callback::run(void (*callback)(int)) { for (unsigned int i = 0; i < 100; i++) { callback(i); } return 0; }
In
callbackClass.hpp
:class callbackClass.hpp { . . . public: int run(void (*callback)(int)); }; #endif
And for
header.h
:int callback(void (*callback)(int));
It's good until logging callback in Swift
side:
func callbackFunc(){
callback({valueFromCallback in //call cpp function
print(valueFromCallback) //Works fine
})
}
But when try to do other stuff like :
func callbackFunc(){
var value : String!
callback({valueFromCallback in //call cpp function
value = String(valueFromCallback) //It has a problem
})
}
Xcode return this error :
A C function pointer cannot be formed from a closure that captures context
I have already seen these questions but did not help:
Swift: Pass data to a closure that captures context
How to cast self to UnsafeMutablePointer<Void> type in swift
A C function pointer cannot be formed from a closure that captures context