Why does this work? In theory, creating a mutable version of an NSData
object by referencing its original bytes using -[NSMutableData dataWithBytesNoCopy:length:freeWhenDone:]
, and then modifying those bytes would result in the original immutable object being modified as well.
It is worth noting that calling -[NSData dataWithBytesNoCopy:length:freeWhenDone:]
results in both data objects having the same byte
pointer address, and that memsetting the data returned by bytes
on an immutable data object will in fact update the data that it represents.
Output:
DataTest[58994:303] original data pointer location: 0x10010a630
DataTest[58994:303] original data: <4c6f7265 6d206970 73756d20 646f6c6f 72207369 7420616d 65742c20 636f6e73 65637465 74757220 61646970 69736369 6e672065 6c69742e>
DataTest[58994:303] mutable data pointer location: 0x100201a00
DataTest[58994:303] mutable data: <4c6f7265 6d206970 73756d20 646f6c6f 72207369 7420616d 65742c20 636f6e73 65637465 74757220 61646970 69736369 6e672065 6c69742e>
DataTest[58994:303] modified mutable data pointer location: 0x100201a00
DataTest[58994:303] modified mutable data: <00000000 00000000 00006d20 646f6c6f 72207369 7420616d 65742c20 636f6e73 65637465 74757220 61646970 69736369 6e672065 6c69742e>
DataTest[58994:303] original data pointer location: 0x10010a630
DataTest[58994:303] original data: <4c6f7265 6d206970 73756d20 646f6c6f 72207369 7420616d 65742c20 636f6e73 65637465 74757220 61646970 69736369 6e672065 6c69742e>
It all boils down to the
-[NSConcreteMutableData initWithBytes:length:copy:deallocator:]
method which is the final constructor that actually creates the instance ofNSMutableData *mutableData
in the code. You can check this by adding a symbolic breakpoint on the method.As for its implementation, it seems the
copy
argument is simply ignored and memory is always copied viamemmove
:)