本文介绍了你如何转换 UnsafeMutablePointer<Void>到UInt8?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这个问题真的很简单,但不知何故我不知道该怎么做:
The question is really simple, but somehow I don't know how to do it:
如何将 UnsafeMutablePointer 转换为 UInt8?
How do you convert a UnsafeMutablePointer to UInt8?
我尝试像这样转换它:
UInt8(theUnsafeMutablePointerVar)
但它给了我以下错误:找不到接受类型为UnsafeMutablePointer"的参数列表的UInt8"类型的初始值设定项
推荐答案
你必须先把指针转换成正确的类型(一个指向 UInt8
的指针)然后你可以访问它指向的内存:
You have to convert the pointer to the correct type first(a pointer to UInt8
) and then you can access the memory it points to:
let u8 = UnsafePointer<UInt8>(theUnsafeMutablePointerVar).memory
在 Swift 3 中, 一个来自 C 的空指针被导入到 Swift 作为UnsafeMutableRawPointer
,可以读取指向的数据与
In Swift 3, a void pointer from C is imported to Swift asUnsafeMutableRawPointer
, and one can read the pointed-to datawith
let u8 = theUnsafeMutablePointerVar.load(as: UInt8.self)
这篇关于你如何转换 UnsafeMutablePointer<Void>到UInt8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!