Sunday, June 2, 2013

NSMutableData replaceBytesInRange example in Objective C (iOS).


NSMutableData replaceBytesInRange

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

- (void)replaceBytesInRange:(NSRange)range withBytes:(const void *)bytes

Parameters
range
The range within the receiver's contents to replace with bytes. The range must not exceed the bounds of the receiver.
bytes
The data to insert into the receiver's contents.

Discussion of [NSMutableData replaceBytesInRange]
If the location of range isn’t within the receiver’s range of bytes, an NSRangeException is raised. The receiver is resized to accommodate the new bytes, if necessary.

A sample using this method is given in Working With Mutable Binary Data.

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

Example of [NSMutableData replaceBytesInRange].
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 example.
    NSRange range = NSMakeRange(0, 1);
[requestData replaceBytesInRange:range withBytes:NULL length:0];

End of NSMutableData replaceBytesInRange example article.