NSMutableData 删除字节? [英] NSMutableData remove bytes?

查看:76
本文介绍了NSMutableData 删除字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用 appendData 方法轻松地向 NSMutableData 实例添加字节,但是我没有看到任何类似的删除数据的方法?我是否忽略了某些东西,还是我需要创建一个新对象并仅复制我需要的字节?

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? Am I overlooking something, or do I need to create a new object and copy over only the bytes I need?

推荐答案

请看以下方法的文档:

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

Apple 明确表示如下:

Apple clearly says the following:

如果范围的长度不等于替换长度,接收器是调整大小以容纳新字节.接收器中超出范围的任何字节被转移以适应新的字节.因此,您可以将 NULL 传递给替换字节和 0 为删除字节的替换长度范围内的接收器.你也可以替换一个范围(这可能为零长度) 的字节数超过范围的长度,它具有插入的效果(或替换一些并插入更多").

If the length of range is not equal to replacementLength, the receiver is resized to accommodate the new bytes. 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. 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").

要从末尾删除 10 个字节,请使用:

To remove 10 byte from the end, use:

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

它也可以通过replaceBytesInRange来完成,但实际上它要快得多,因为字节并没有真正被删除.相反,只更改了内部大小变量,并且 NSMutableData 的行为就像删除了字节一样.IOW,这是一个 O(1) 操作(这意味着它总是需要同样长的时间来执行,无论您删除多少字节),而且速度非常快.

It could also be done via replaceBytesInRange, but it's in fact much faster, because the bytes are not really removed. Instead only the internal size variable is changed and NSMutableData will behave as if the bytes were removed. 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.

要从前面删除 10 个字节,请使用:

To remove 10 byte from front, use:

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

要删除中间的 10 个字节(例如在 20 个字节之后),请使用:

To remove 10 bytes in the middle (e.g. after 20 bytes), use:

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

replaceBytesInRange 是一个 O(n) 操作,不过.这意味着无论删除 100 个字节需要多长时间,删除 200 个字节都需要两倍的时间,依此类推.它仍然非常快,并且仅受计算机内存 (RAM) 吞吐量的限制.如果您有 10 MB 的数据并且从前面删除了 1 MB,则会复制 9 MB 以填补刚刚删除的 MB 的空白.因此,操作的速度取决于您的系统将 9 MB RAM 从一个地址移动到另一个地址的速度.这通常足够快,除非您处理包含数百 MB 的 NSMutableData 对象.

replaceBytesInRange is a O(n) operation, though. 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. It is still pretty fast and only limited by the throughput of your computer's memory (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. So the speed of the operation depends on how fast can your system move 9 MB of RAM from one address to another one. And this is usually fast enough, unless you deal with NSMutableData objects containing hundreds of MB.

这篇关于NSMutableData 删除字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆