Sunday, June 2, 2013

NSMutableData replaceBytesInRange withBytes example in Objective C (iOS).


NSMutableData replaceBytesInRange withBytes

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 withBytes]
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 withBytes example.
[source setLength:myStart + myLength];
[source replaceBytesInRange:NSMakeRange(0, myStart)
                  withBytes:NULL
                     length:0];

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

End of NSMutableData replaceBytesInRange withBytes example article.