【发布时间】:2015-07-25 12:24:22
【问题描述】:
这个问题很简单,但不知怎么做:
如何将 UnsafeMutablePointer 转换为 UInt8?
我尝试像这样转换它:
UInt8(theUnsafeMutablePointerVar)
但它给了我以下错误:找不到接受类型为“UnsafeMutablePointer”的参数列表的“UInt8”类型的初始化程序
【问题讨论】:
标签: swift type-conversion
这个问题很简单,但不知怎么做:
如何将 UnsafeMutablePointer 转换为 UInt8?
我尝试像这样转换它:
UInt8(theUnsafeMutablePointerVar)
但它给了我以下错误:找不到接受类型为“UnsafeMutablePointer”的参数列表的“UInt8”类型的初始化程序
【问题讨论】:
标签: swift type-conversion
您必须先将指针转换为正确的类型
(指向UInt8 的指针),然后您可以访问它指向的内存:
let u8 = UnsafePointer<UInt8>(theUnsafeMutablePointerVar).memory
在 Swift 3 中, 来自 C 的 void 指针被导入到 Swift
UnsafeMutableRawPointer,可以读取指向的数据
与
let u8 = theUnsafeMutablePointerVar.load(as: UInt8.self)
【讨论】:
UnsafePointer<UInt8>(theUnsafeMutablePointerVar) 对我不起作用。 Cannot invoke initializer for type 'UnsafePointer<UInt8>' with an argument list of type '(UnsafeMutablePointer<Void>)'。如果我尝试 UnsafeMutablePointer<UInt8> 而不是 UnsafePointer 也是一样。
theUnsafeMutablePointerVar 是什么类型?
let u8 = UnsafePointer<UInt8>(theUnsafeMutablePointerVar).memory,然后在需要的地方使用u8。它帮助了我。奇怪!
在最新的 Swift 中使用 pointee 属性。
【讨论】: