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?
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- back button text does not change
- iOS (objective-c) compression_decode_buffer() retu
- how to find the index position of the ARRAY Where
相关文章
- 现在使用swift开发ios应用好还是swift?
- TCC __TCCAccessRequest_block_invoke
- xcode 4 garbage collection removed?
- Unable to process app at this time due to a genera
- How can I add media attachments to my push notific
- didBeginContact:(SKPhysicsContact *)contact not in
- Custom Marker performance iOS, crash with result “
- Why is my library not able to expand on the CocoaP
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
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:
This works great.
Since NSMutableData is toll-free bridged with CFMutableDataRef, you can use the CFDataDeleteBytes() function:
Please see the documentation of the following method:
Apple clearly says the following:
To remove 10 byte from the end, use:
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.
To remove 10 byte from front, use:
To remove 10 bytes in the middle (e.g. after 20 bytes), use:
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.