Note that there are some explanatory texts on larger screens.

plurals
  1. PONew NSData with range of old NSData maintaining bytes
    primarykey
    data
    text
    <p>I have a fairly large NSData (or NSMutableData if necessary) object which I want to take a small chunk out of and leave the rest. Since I'm working with large amounts of NSData bytes, I don't want to make a big copy, but instead just truncate the existing bytes. Basically:</p> <ul> <li>NSData *source: &lt; a few bytes I want to discard > + &lt; big chunk of bytes I want to keep ></li> <li>NSData *destination: &lt; big chunk of bytes I want to keep ></li> </ul> <p>There are truncation methods in NSMutableData, but they only truncate the end of it, whereas I want to truncate the beginning. My thoughts are to do this with the methods:</p> <p><strong><em>Note that I used the wrong (copying) method in the original posting. I've edited and fixed it</em></strong></p> <pre><code>- (const void *)bytes </code></pre> <p>and</p> <pre><code>- initWithBytesNoCopy:length:freeWhenDone: </code></pre> <p>However, I'm trying to figure out how to manage memory with these. I'm guessing the process will be like this (I've placed ????s where I don't know what to do):</p> <pre><code>// Get bytes const unsigned char *bytes = (const unsigned char *)[source bytes]; // Offset the start bytes += myStart; // Somehow (m)alloc the memory which will be freed up in the following step ????? // Release the source, now that I've allocated the bytes [source release]; // Create a new data, recycling the bytes so they don't have to be copied NSData destination = [[NSData alloc] initWithBytesNoCopy:bytes length:myLength freeWhenDone:YES]; </code></pre> <p>Thanks for the help!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload