Sunday, June 2, 2013

NSMutableData resetBytesInRange example in Objective C (iOS).

NSMutableData resetBytesInRange

Replaces with zeroes the contents of the receiver in a given range.

- (void)resetBytesInRange:(NSRange)range

The range within the contents of the receiver to be replaced by zeros. The range must not exceed the bounds of the receiver.

Discussion of [NSMutableData resetBytesInRange]
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.

NSMutableData resetBytesInRange example.
NSMutableData *imgData = [NSMutableData data]; /* note, we do not specify a capacity--it's pointless for this use case */
[imgData setData: UIImageJPEGRepresentation(img, 1.0)];
[imgData resetBytesInRange: NSMakeRange(0, [imgData length])];

NSMutableData *imgData = [[UIImageJPEGRepresentation(img, 1.0) mutableCopy] autorelease];
[imgData resetBytesInRange: NSMakeRange(0, [imgData length])];

Example of [NSMutableData resetBytesInRange].
NSMutableData *postData = [[NSMutableData alloc] init];    [postData resetBytesInRange:NSMakeRange(0, [postData length])];
    [postData setLength:0];

NSMutableData *orgData = [[NSMutableData alloc] init];
    [orgData resetBytesInRange:NSMakeRange(0, [orgData length])];
    [orgData setLength:0];

    orgData = (NSMutableData *)[userInfo dataUsingEncoding:NSUTF8StringEncoding];
    [postData appendBytes:[orgData bytes] length:100];
    return postData;

End of NSMutableData resetBytesInRange example article.