如何在Swift中打印指针内容

时间:2018-01-05 15:22:53

标签: ios swift unsafemutablepointer

我正在尝试打印由原始指针指向的指针的内容,但是当我打印或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
        }
    }

1 个答案:

答案 0 :(得分:2)

pixel是指向UInt32的指针,用于打印指向的 你必须取消引用它的价值:

print(pixel.pointee)

请注意,递增指针是以步幅为单位完成的 指向的值,所以你的

pixel = pixel + kBytesPerPixel

将地址递增4 * kBytesPerPixel个字节,这是 可能不是你想要的。

相关问题