Is it possible to convert a Swift class into C void* pointer?
// swift class
class MyClass {
}
var myClass = MyClass()
var p: UnsafeMutablePointer<Void> = myClass //<- failed
//c function
void someSwiftClass(void *context);
Thanks
I had similar problem, you can convert this as below.
var myClass = MyClass()
var classPtr = unsafeBitCast(myClass, UnsafePointer<Void>.self)
and your function,
func someSwiftClass(voidPointer: UnsafeMutablePointer<Void>) {
}
If you want to declare constant pointer use UnsafePointer<T>
and when your pointer is not constant use UnsafeMutablePointer<T>
In C constant pointer - const int *myCpointer
Corresponding swift pointer - let mySwiftPointer: UnsafePointer<Int32>
This works:
var p: UnsafeMutablePointer<Void> =
UnsafeMutablePointer(Unmanaged.passUnretained(myClass).toOpaque())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With