I don't quite understand why RJ is suggesting to use CoreFoundation, when NSMutableData has actually a method for exactly that. Please read the documentation of (void)replaceBytesInRange:(NSRange)range withBytes:(const void *)replacementBytes length:(NSUInteger)replacementLength Apple clearly says the following: 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�) To remove 10 byte from the end, use: 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. 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: data replaceBytesInRange:NSMakeRange(0, 10) withBytes:NULL length:0 To remove 10 bytes in the middle (e.g. After 20 bytes), use: data replaceBytesInRange:NSMakeRange(20, 10) withBytes:NULL length:0 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.
I don't quite understand why RJ is suggesting to use CoreFoundation, when NSMutableData has actually a method for exactly that. Please read the documentation of - (void)replaceBytesInRange:(NSRange)range withBytes:(const void *)replacementBytes length:(NSUInteger)replacementLength Apple clearly says the following: 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�). To remove 10 byte from the end, use: 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.
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: data replaceBytesInRange:NSMakeRange(0, 10) withBytes:NULL length:0; To remove 10 bytes in the middle (e.g. After 20 bytes), use: data replaceBytesInRange:NSMakeRange(20, 10) withBytes:NULL length:0; 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.
Since NSMutableData is toll-free bridged with CFMutableDataRef, you can use the CFDataDeleteBytes() function: NSMutableData *data = ... CFDataDeleteBytes((CFMutableDataRef)data, CFRangeMake(3, 4)).
1 Be careful. Down this path lies performance woes. If you are editing relatively few times or working with a small bit of data, no big deal.
Anything larger or frequent or performance sensitive, consider using an optimized data structure of some type. – bbum Feb 27 '10 at 2:33 Thanks, this worked great. How do you know that NSMutable data is 'toll-free bridged' with CFMutableDataRef?
I do not see any mention of this in the NSMutableData docs? – Zenox Feb 27 '10 at 11:47 If you look at the "Overview" section of the NSMutableData docs developer.apple. Com/mac/library/DOCUMENTATION/Cocoa/Reference/… you'll see it mentioned in the second paragraph – RJ.
Mar 1 '10 at 17:50 1 Why using CoreFoundation, if you can use the NSMutableData method replaceBytesInRange, which according to documentation removes the bytes in range, if the replacement byte pointer is NULL and the replacement length is also 0? – Mecki Aug 3 '10 at 21:59 1 While this solution works, I think Mecki's is the cleaner and should be marked the accepted. +1 to Mecki, but no ding here foe a find 2nd best solution.(it would be interesting to know if, under the covers, one solution uses the other!
:) – Olie Dec 10 '10 at 16:35.
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 NSMutableDataInstance. Length -= n; for anything more complicated than that I'd recommend manipulating the raw data.
Thanks for the tip! I am however trying to remove from the front :) – Zenox Feb 27 '10 at 11:39.
Resized to accommodate the new bytes. The receiver in the range range. And insert more”).
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 bytes in the middle (e.g. 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.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.