かなり大きな NSData (または必要に応じて NSMutableData ) オブジェクトがあり、そこから小さなチャンクを取り出して残りを残したいと考えています。私は大量の NSData バイトを扱っているので、大きなコピーを作成したくはありませんが、代わりに既存のバイトを切り捨てます。基本的:
- NSData *ソース: < 破棄したい数バイト > + < 保持したい大きなバイト >
- NSData *destination: <保持したい大量のバイト>
NSMutableData には切り捨てメソッドがありますが、それらは末尾のみを切り捨てますが、私は先頭を切り捨てたいと考えています。私の考えは、メソッドでこれを行うことです:
元の投稿で間違った(コピー)方法を使用したことに注意してください。編集して修正しました
- (const void *)bytes
と
- initWithBytesNoCopy:length:freeWhenDone:
ただし、これらを使用してメモリを管理する方法を理解しようとしています。プロセスは次のようになると思います(何をすべきかわからない場所に????sを配置しました):
// 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];
助けてくれてありがとう!