Are Data/NSData bytes guaranteed to be stored in contiguous memory? Looking at the documentation (https://developer.apple.com/reference/foundation/data), I'm under the impression that one can access a contiguous representation of the bytes using
No, data is not guaranteed to be stored in contiguous memory. If you use
withUnsafeMutableBytes, though, it will copy it to a a location which will have the data in single contiguous block. If you don't want to incur that overhead, it's better to call
enumerateBytes, which will, if the data is not currently contiguous, will call the closure for once for each of the separate blocks of memory, avoiding the overhead of copying of the full data to a new contiguous block.