[英]How to print pointer contents in Swift
我正在尝试打印原始指针指向的指针的内容,但是当我打印或NSLog时,我得到的指针的值要比指针指向的内存的内容大。 如何打印指针指向的内存内容? 下面是我的代码:
let buffer = unsafeBitCast(baseAddress, to: UnsafeMutablePointer<UInt32>.self)
for row in 0..<bufferHeight
{
var pixel = buffer + row * bytesPerRow
for _ in 0..<bufferWidth {
// NSLog("Pixel \(pixel)")
print(pixel)
pixel = pixel + kBytesPerPixel
}
}
pixel
是指向UInt32
的指针,为了打印指向的值,您必须取消引用它:
print(pixel.pointee)
请注意,以指向值的跨度为单位来增加指针,因此您的
pixel = pixel + kBytesPerPixel
会将地址增加4 * kBytesPerPixel
字节,这可能不是您想要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.