【问题标题】:How to print pointer contents in Swift如何在 Swift 中打印指针内容
【发布时间】:2018-01-05 15:22:52
【问题描述】:

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

【问题讨论】:

    标签: ios swift unsafemutablepointer


    【解决方案1】:

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

    print(pixel.pointee)
    

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

    pixel = pixel + kBytesPerPixel
    

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

    【讨论】:

    • 好的,我用 UInt8 替换了 UInt32,所以指针现在是 UInt8 。如何将此 UInt8 指针类型转换为 UInt32 * 然后打印整数?
    猜你喜欢
    • 2020-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 2021-03-27
    • 1970-01-01
    相关资源
    最近更新 更多