将值输出到 swift 中的 typedef 指针 [英] outputting a value to a typedef pointer in swift
问题描述
我几乎可以肯定这个标题不正确,但这里是...
I'm almost certain the title of this isn't correct but here goes...
我正在桥接到一个 Objective-C 类来设置一个 typedef.桥已设置好,我可以正确声明 typedef var.
I'm bridging to an Objective-C class to set a typedef. The bridge is set up and I'm able to declare the typedef var correctly.
在 Objective-C 中,我还从同一个类中调用了一个方法,该方法在调用时会向变量 TestHandle 输出一个值.
In Objective-C I also called a method from the same class that, when called, output a value to the variable TestHandle.
var TestHandle : TESTHANDLE
TestInit(&TestHandle)
当我使用 Swift 5 尝试此操作时,出现此错误:
When I try this using Swift 5 I get this error:
Cannot convert value of type 'inout TESTHANDLE' (aka 'inout UnsafeMutableRawPointer') to expected argument type 'UnsafeMutablePointer<TESTHANDLE?>?' (aka 'Optional<UnsafeMutablePointer<Optional<UnsafeMutableRawPointer>>>')
有什么指点吗?
推荐答案
一些观察:
TESTHANDLE
似乎是UnsafeMutableRawPointer
的别名&testHandle
正在获取testHandle
的引用(指向位置的指针),生成inout UnsafeMutableRawPointer
类型的值/li>- 正如错误所说,您的
TestInit
函数采用UnsafeMutablePointer<TESTHANDLE?>?
类型的变量,又名Optional
TESTHANDLE
appears to be an alias forUnsafeMutableRawPointer
&testHandle
is taking a reference (a pointer to the location) of thetestHandle
, producing a value of typeinout UnsafeMutableRawPointer
- As the error says, your
TestInit
function takes a variable of typeUnsafeMutablePointer<TESTHANDLE?>?
, a.k.a.Optional<UnsafeMutablePointer<Optional<UnsafeMutableRawPointer>>>
Swift 有一些关于 &
如何自动桥接各种指针类型的规则,但坦率地说,我不太了解它们.
Swift has some rules about how &
automatically bridges to the various pointer types, but to be frank, I don't understand them very well.
据我所知,Swift 指针类型不能表示 nil (0x000...000).要做到这一点,它们需要包含在一个可选项中.所以当你看到类型
As far as I know, the Swift pointer types cannot represent nil (0x000...000). To do that, they need to be wrapped within an optional. So when you see the type
Optional<UnsafeMutablePointer<Optional<UnsafeMutableRawPointer>>>
它实际上是两个语义"部分:
It's actually two "semantic" parts:
Optional<UnsafeMutablePointer< Optional<UnsafeMutableRawPointer> >>
↳ A nullable pointer to ... ↳ ... something that's a nullable pointer of unspecified (void) type
您收到错误的原因是 &testHandle
只能将您的 UnsafeMutableRawPointer
桥接到 Optional<UnsafeMutablePointer<UnsafeMutableRawPointer>>
code>,但不是必需的 Optional
(区别在于缺少内部"可空性层).要解决此问题,请自行设置 testHandle
可选:
The reason you're getting your error is because &testHandle
can only bridge your UnsafeMutableRawPointer
to a Optional<UnsafeMutablePointer<UnsafeMutableRawPointer>>
, but not the required Optional<UnsafeMutablePointer<Optional<UnsafeMutableRawPointer>>>
(the difference is in that missing layer of "inner" nullability). To get around this, make your testHandle
optional, yourself:
var testHandle: TESTHANDLE? // a.k.a. Optional<TESTHANDLE>, a.k.a. Optional< UnsafeMutableRawPointer>
然后,当您使用 &
运算符时,Swift 会将您的值包装在所需的 Optional<UnsafeMutablePointer<... >>
外层.
Then, when you use the &
operator, Swift will wrap your value in the required Optional<UnsafeMutablePointer< ... >>
outter layer.
typealias TESTHANDLE = UnsafeMutableRawPointer
func testInit(_ p: UnsafeMutablePointer<TESTHANDLE?>?) {
print("Success!")
}
var testHandle: TESTHANDLE? = nil
testInit(&testHandle)
这篇关于将值输出到 swift 中的 typedef 指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!