[swift-users] Extracting arbitrary types (e.g. UInt16) out of Data

Joe Groff jgroff at apple.com
Mon Jun 26 12:52:32 CDT 2017


> On Jun 26, 2017, at 10:05 AM, Philippe Hausler <phausler at apple.com> wrote:
> 
> Data.copyBytes will do that under the hood
> 
> var crc: UInt16 = 0
> let amountCopied = withUnsafeMutablePointer(to: &crc) { data.copyBytes(to: UnsafeMutableBufferPointer(start: $0, count: 1)) }
> if amountCopied == MemoryLayout<UInt16>.size {
>    // we have a full crc
> }
> 
> That will probably do what you want; plus it will allow you to do it from a given range of bytes.

Cool. That'd be a nice API to backport to *BufferPointer too!

-Joe


More information about the swift-users mailing list