[swift-users] Slicing a [UInt8] into a Data without copying?

Philippe Hausler phausler at apple.com
Thu May 4 18:44:33 CDT 2017


Not sure on exactly how you are wanting to use this but you could approach this like:

let bytes: [UInt8] = [ 0xC0, 0xC0, 0xA1, 0x00, 0xC0, 0xC0, 0xA1, 0x00]
let slice = bytes[0..<4]
slice.withUnsafeBytes { buffer in
    let d = Data(bytesNoCopy: UnsafeMutableRawPointer(mutating: buffer.baseAddress!), count: buffer.count, deallocator: .none)
    // whatever you do, dont let d escape
}



> On May 4, 2017, at 16:31, Rick Mann via swift-users <swift-users at swift.org> wrote:
> 
> Is it possible to make a (immutable) Data() object from a slice of a [UInt8] and avoid copying the data?
> 
> -- 
> Rick Mann
> rmann at latencyzero.com
> 
> 
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170504/7e9eeef0/attachment.html>


More information about the swift-users mailing list