简体   繁体   English

NSMutableData删除字节?

[英]NSMutableData remove bytes?

I can add bytes to a NSMutableData instance easily by using the appendData method, however I do not see any similar method for removing data? 我可以使用appendData方法轻松地向NSMutableData实例添加字节,但是我没有看到任何类似的删除数据的方法? Am I overlooking something, or do I need to create a new object and copy over only the bytes I need? 我忽略了什么,或者我是否需要创建一个新对象并仅复制我需要的字节?

Please see the documentation of the following method: 请参阅以下方法的文档:

- (void)replaceBytesInRange:(NSRange)range withBytes:(const void *)replacementBytes length:(NSUInteger)replacementLength

Apple clearly says the following: Apple明确说明如下:

If the length of range is not equal to replacementLength, the receiver is resized to accommodate the new bytes. 如果range的长度不等于replacementLength,则调整接收器的大小以容纳新的字节。 Any bytes past range in the receiver are shifted to accommodate the new bytes. 接收器中超出范围的任何字节都会移位以容纳新字节。 You can therefore pass NULL for replacementBytes and 0 for replacementLength to delete bytes in the receiver in the range range. 因此,您可以为replacementBytes传递NULL,为replacementLength传递0以删除范围范围内接收器中的字节。 You can also replace a range (which might be zero-length) with more bytes than the length of the range, which has the effect of insertion (or “replace some and insert more”). 您还可以使用比范围长度更多的字节来替换范围(可能是零长度),这具有插入的效果(或“替换一些并插入更多”)。

To remove 10 byte from the end, use: 要从末尾删除10个字节,请使用:

[data setLength:[data length] - 10];

It could also be done via replaceBytesInRange, but it's in fact much faster, because the bytes are not really removed. 它也可以通过replaceBytesInRange来完成,但它实际上要快得多,因为字节并没有真正被删除。 Instead only the internal size variable is changed and NSMutableData will behave as if the bytes were removed. 而是仅更改内部大小变量,并且NSMutableData的行为就像删除了字节一样。 IOW, this is a O(1) operation (that means it will always take equally long to perform, regardless of how many bytes you remove), and it is very fast. IOW,这是一个O(1)操作(这意味着无论你删除多少字节,它总是需要相同的长时间执行),而且速度非常快。

To remove 10 byte from front, use: 要从前面删除10个字节,请使用:

[data replaceBytesInRange:NSMakeRange(0, 10) withBytes:NULL length:0];

To remove 10 bytes in the middle (eg after 20 bytes), use: 要删除中间的10个字节(例如,在20个字节之后),请使用:

[data replaceBytesInRange:NSMakeRange(20, 10) withBytes:NULL length:0];

replaceBytesInRange is a O(n) operation, though. 但是,replaceBytesInRange是一个O(n)操作。 That means no matter how long it takes to remove 100 byte, it will take twice as long to remove 200 bytes and so on. 这意味着无论移除100个字节需要多长时间,移除200个字节需要两倍的时间,依此类推。 It is still pretty fast and only limited by the throughput of your computer's memory (RAM). 它仍然非常快,仅受计算机内存(RAM)吞吐量的限制。 If you have 10 MB of data and you remove 1 MB from front, 9 MB are copied to fill the gap of the just removed MB. 如果您有10 MB的数据,并从前面删除1 MB,则复制9 MB以填补刚刚删除的MB的间隙。 So the speed of the operation depends on how fast can your system move 9 MB of RAM from one address to another one. 因此,操作的速度取决于系统将9 MB RAM从一个地址移动到另一个地址的速度。 And this is usually fast enough, unless you deal with NSMutableData objects containing hundreds of MB. 这通常足够快,除非您处理包含数百MB的NSMutableData对象。

Since NSMutableData is toll-free bridged with CFMutableDataRef, you can use the CFDataDeleteBytes() function: 由于NSMutableData使用CFMutableDataRef进行免费桥接,因此您可以使用CFDataDeleteBytes()函数:

NSMutableData *data = ...
CFDataDeleteBytes((CFMutableDataRef)data, CFRangeMake(3, 4));

If the data you want to remove is at the end, you can use 如果要删除的数据在最后,则可以使用

[NSMutableDataInstance setLength:[NSMutableDataInstance length] - n];

or with the obj-c 2.0 syntax 或使用obj-c 2.0语法

NSMutableDataInstance.length -= n;

for anything more complicated than that I'd recommend manipulating the raw data. 对于比我更复杂的事情,我建议操纵原始数据。

I see that this thread has the answer, but nevertheless this could be valuable add. 我看到这个帖子有答案,但不过这可能是有价值的补充。 To remove all bytes from NSMuttableData, you can create new Data and copy it to original: 要从NSMuttableData中删除所有字节,您可以创建新数据并将其复制到原始数据:

[myData setData:[NSData dataWithBytes:NULL length:0]];

This works great. 这非常有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM