Swift 5 update problem: UnsafeRawBufferPointer vs UnsafePointer

We are using Apple's Compression framework to uncompress Data:

let uncompressedSize = compressedData.withUnsafeBytes { compressedBuffer in
    compression_decode_buffer(
        uncompressedBuffer,
        uncompressedSize,
        compressedBuffer,
        compressedBufferSize,
        nil,
        COMPRESSION_LZFSE
    )
}

Swift 5 compiler complains:

'withUnsafeBytes' is deprecated: use withUnsafeBytes<R>(_: (UnsafeRawBufferPointer) throws -> R) rethrows -> R instead

The Compression API expects UnsafePointer<UInt8> but the new API on Data provides UnsafeRawBufferPointer. How to fix this?

P.S. I also wonder why the documentation for Data doesn't include the new method:

4 Likes

I think this thread is someone else asking the same question:

1 Like

All right, this one helped:

let unsafeBufferPointer = unsafeRawBufferPointer.bindMemory(to: UInt8.self)
let unsafePointer = unsafeBufferPointer.baseAddress!

From: https://mjtsai.com/blog/2019/03/27/swift-5-released/

2 Likes
Terms of Service

Privacy Policy

Cookie Policy