In previous betas, I was able to pass a pointer to anything to C doing something like this
在以前的测试版中,我能够将指向任何东西的指针传递给C做类似的事情
var aString : NSString = "This is a string" // create an NSString
var anUnmanaged = Unmanaged.passUnretained(aString) // take an unmanaged pointer
var opaque : COpaquePointer = anUnmanaged.toOpaque() // convert it to a COpaquePointer
var mut : CMutablePointer = &opaque // this is a CMutablePointer
doSomething(mut) // pass the pointer to an helper function written in C
With the C function like this:
使用C函数是这样的:
void doSomething(void ** ptr)
{
// do something with the pointer, change it and make it point to another nsstring for example
}
Now, (for sure in Beta4, maybe even in Beta3) the syntax translated from C to Swift has changed
现在,(肯定在Beta4中,甚至可能在Beta3中)从C转换为Swift的语法已经改变
Before, the doSomething function was expecting a CMutablePointer (that now has been removed from Swift) Now, it expects a:
之前,doSomething函数期待CMutablePointer(现在已从Swift中删除)现在,它期望:
UnsafePointer<UnsafePointer<()>>
() is a typealias to Void
()是Void的类型
So, syntactically talking is more clear. However, Now I can't understand how to take the void** pointer of any object like I was doing in previous betas.
所以,语法上的说话更清楚。但是,现在我无法理解如何获取任何对象的void **指针,就像我在之前的测试版中所做的那样。
Any suggestion?
3 个解决方案
#1
2
This worked for me:
这对我有用:
var str = "Hello, playground"
var p: UnsafePointer<Void> = unsafeAddressOf(str)
#2
1
Not sure if this is the correct answer, I hope that someone has a better method to do this. The main problem was that there is no direct conversion between a Swift Object and a Void UnsafePointer ( UnsafePointer<()>
) However I managed how to do it passing through a COpaquePointer
不确定这是否是正确的答案,我希望有人有更好的方法来做到这一点。主要问题是Swift对象和Void UnsafePointer(UnsafePointer <()>)之间没有直接转换但是我管理了如何通过COpaquePointer进行转换
// since I can't go directly from NSMutableString to UnsafePointer<()> (which is UnsafePointer<Void>) i pass through a COpaquePointer
var aString : NSMutableString = NSMutableString(string: "test") // create an NSString
var anUnmanaged = Unmanaged.passUnretained(aString) // take an unmanaged pointer
var opaque : COpaquePointer = anUnmanaged.toOpaque() // convert it to a COpaquePointer
var anUnsafe : UnsafePointer<()> = UnsafePointer(opaque) // convert it to an UnsafePointer to Void ( () is a Typealias to Void) using the init(_ other: COpaquePointer) initializer
doSomething(&anotherUnsafe)
#3
0
Can you try this:
你能试试这个:
let strPtr = unsafeBitCast(aString, UnsafePointer<Void>.self)
let strPtrPtr = unsafeBitCast(strPtr, UnsafePointer<Void>.self)
And then:
doSomething(strPtrPtr)
#1
2
This worked for me:
这对我有用:
var str = "Hello, playground"
var p: UnsafePointer<Void> = unsafeAddressOf(str)
#2
1
Not sure if this is the correct answer, I hope that someone has a better method to do this. The main problem was that there is no direct conversion between a Swift Object and a Void UnsafePointer ( UnsafePointer<()>
) However I managed how to do it passing through a COpaquePointer
不确定这是否是正确的答案,我希望有人有更好的方法来做到这一点。主要问题是Swift对象和Void UnsafePointer(UnsafePointer <()>)之间没有直接转换但是我管理了如何通过COpaquePointer进行转换
// since I can't go directly from NSMutableString to UnsafePointer<()> (which is UnsafePointer<Void>) i pass through a COpaquePointer
var aString : NSMutableString = NSMutableString(string: "test") // create an NSString
var anUnmanaged = Unmanaged.passUnretained(aString) // take an unmanaged pointer
var opaque : COpaquePointer = anUnmanaged.toOpaque() // convert it to a COpaquePointer
var anUnsafe : UnsafePointer<()> = UnsafePointer(opaque) // convert it to an UnsafePointer to Void ( () is a Typealias to Void) using the init(_ other: COpaquePointer) initializer
doSomething(&anotherUnsafe)
#3
0
Can you try this:
你能试试这个:
let strPtr = unsafeBitCast(aString, UnsafePointer<Void>.self)
let strPtrPtr = unsafeBitCast(strPtr, UnsafePointer<Void>.self)
And then:
doSomething(strPtrPtr)