Is NSData/Data storage contiguous?

No, Data is not guaranteed to be stored in contiguous memory. If you use withUnsafeBytes or withUnsafeMutableBytes, though, it will copy all the buffers to a single contiguous buffer.

If you do not want to incur that overhead, you can access the individual buffers using the regions property.


The presence of withUnsafeBytes is a way of documenting that they reserve the right to not store the bytes contiguously, but can provide them that way on demand.

This might be because they imagine NSData would be ported to memory constrained environments and they don't want you to have system dependent code.

Tags:

Swift