该文件说:“当一个函数被声明为接受的UnsafeMutablePointer的说法,它可以接受任何其操作数的类型类型,这是作为左值的转交地址的存储左值以下...一个进出的表达“。 我可以复制的例子,证明这一点。
func takesAMutablePointer<T>(x: UnsafeMutablePointer<T>) -> UnsafeMutablePointer<T>
{
return x
}
func useAMutablePointer()
{
var legInt :UInt32 = 42
var legIntArray: [UInt32] = [legInt]
var legIntPtr: UnsafeMutablePointer<UInt32>
legIntPtr = takesAMutablePointer(&legInt)
legIntPtr = takesAMutablePointer(&legIntArray)
legIntPtr = UnsafeMutablePointer(&legInt) //Compile error!
legIntPtr = UnsafeMutablePointer(&legIntArray) //Compile error!
}
Xcode中显示,UnsafeMutablePointer有以下初始化:
init<U>(_ from: UnsafeMutablePointer<U>)
但是当我尝试使用它就像我的函数,编译错误是Cannot find an initializer for type 'UnsafeMutablePointer<T>' that accepts an argument list of type '(inout UInt32)'
所以,
- 有什么直接的方式来获得一个
UnsafeMutablePointer<UInt32>
到legInt? - 为什么我不能用初始化如预期?
谢谢!