Sunday, June 2, 2013

NSMutableData replaceBytesInRange withBytes length example in Objective C (iOS).


NSMutableData replaceBytesInRange withBytes length

Replaces with a given set of bytes a given range within the contents of the receiver.

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

Parameters of [NSMutableData replaceBytesInRange withBytes length]
range
The range within the receiver's contents to replace with bytes. The range must not exceed the bounds of the receiver.
replacementBytes
The data to insert into the receiver's contents.
replacementLength
The number of bytes to take from replacementBytes.

Discussion of [NSMutableData replaceBytesInRange withBytes length]
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”).

NSMutableData replaceBytesInRange withBytes length example.
[source setLength:myStart + myLength];
[source replaceBytesInRange:NSMakeRange(0, myStart)
                  withBytes:NULL
                     length:0];

Example of [NSMutableData replaceBytesInRange withBytes length].
I think you want to do something like this:

[soundFileData replaceBytesInRange:NSMakeRange(4, 4)
                         withBytes:&(UInt32){NSSwapHostIntToLittle(totalLength-8)}];
[soundFileData replaceBytesInRange:NSMakeRange(42, 4)
                         withBytes:&(UInt32){NSSwapHostIntToLittle(totalLength)}];

NSMutableData replaceBytesInRange withBytes length example.
    NSRange range = NSMakeRange(0, 1);
[requestData replaceBytesInRange:range withBytes:NULL length:0];

End of NSMutableData replaceBytesInRange withBytes length example article.