Saturday, June 1, 2013

NSData initWithBytesNoCopy length example in Objective C (iOS).

NSData initWithBytesNoCopy length

Returns a data object initialized by adding to it a given number of bytes of data from a given buffer.

- (id)initWithBytesNoCopy:(void *)bytes length:(NSUInteger)length

A buffer containing data for the new object. bytes must point to a memory block allocated with malloc.
The number of bytes to hold from bytes. This value must not exceed the length of bytes.

Return Value of [NSData initWithBytesNoCopy length]
A data object initialized by adding to it length bytes of data from the buffer bytes. The returned object might be different than the original receiver.

Discussion of [NSData initWithBytesNoCopy length]
The returned object takes ownership of the bytes pointer and frees it on deallocation. Therefore, bytes must point to a memory block allocated with malloc.

NSData initWithBytesNoCopy length example.
int offset = 10;
NSRange dataRange;
dataRange.length = [data length] - offset;
dataRange.location = offset;

void *buffer = malloc(dataRange.length);
[data getBytes:buffer range:dataRange];
self.bodyData = [[NSData alloc] initWithBytesNoCopy:buffer length:dataRange.length];
free(buffer); //Make sure your property has either retain attribute (or strong if using ARC)

Example of [NSData initWithBytesNoCopy length].
if (flags == nil) {
   flagPtr *flgH = &flags;
   NSData *flgsDatum =
      [[NSData alloc] initWithBytesNoCopy:flgH length:sizeof(flgH)
   [[NSNotificationCenter defaultCenter] postNotificationName:@"fetchFlags"
   *flags = (WORKING | (*flags & LOGFILE));

NSData initWithBytesNoCopy length example.
- (NSString *)hashWithDigest:(AUMessageDigest)digest data:(NSData **)data
    u_byte *(*CC_XXX)(const void *, CC_LONG, u_byte *);
    u_byte *md = NULL;
    NSInteger i = 0;
    NSString *result;
    NSMutableString *buffer;
    switch (digest) {
        case AUDigestSHA1:
            CC_XXX = CC_SHA1;
        case AUDigestSHA256:
            CC_XXX = CC_SHA256;
        case AUDigestSHA512:
            CC_XXX = CC_SHA512;
        case AUDigestMD5:
            CC_XXX = CC_MD5;
            return nil;
    result = nil;
    if((md = malloc(CC_XXX_DIGEST_LEN * sizeof(u_byte) + 1))) {
        memset(md, 0, CC_XXX_DIGEST_LEN + 1);
        CC_XXX([self bytes], [self length], md);
        buffer = [NSMutableString string];
        for (i = 0; i< CC_XXX_DIGEST_LEN; i++) {
            [buffer appendFormat:@"%02x", (CC_LONG)(md[i])];
        if (data != nil) {
            *data = [[NSData alloc] initWithBytesNoCopy:md length:CC_XXX_DIGEST_LEN freeWhenDone:YES];
        } else {
        result = [NSString stringWithString:buffer];
    return result;

End of NSData initWithBytesNoCopy length example article.