Showing posts with label NSData initWithBytes length example. Show all posts
Showing posts with label NSData initWithBytes length example. Show all posts

Saturday, June 1, 2013

NSData initWithBytes length example in Objective C (iOS).


NSData initWithBytes length

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

- (id)initWithBytes:(const void *)bytes length:(NSUInteger)length

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

NSData initWithBytes length example.
//This is just mock up data to represent what would be passed into your method
unsigned char ch1[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x04, 0x01, 0x00, 0x0F };
NSData *data1 = [[NSData alloc] initWithBytes:ch1
                                       length:sizeof(ch1)];
//This is the data used for the comparison
NSData *data2 = [[NSData alloc] initWithBytes:(unsigned char[]){0x04, 0x01, 0x00}
                                       length:3];

NSRange range = [data1 rangeOfData:data2
                           options:0
                             range:NSMakeRange(0, [data1 length])];

if(range.location != NSNotFound)
{
     NSLog(@"Found pattern!");
}

Example of [NSData initWithBytes length].
cat.catName = (catName)?[NSString stringWithUTF8String:catName]:@"";
NSData *dataForCachedImage = [[NSData alloc] initWithBytes:sqlite3_column_blob(statement, 2) length: sqlite3_column_bytes(statement, 2)];          
cat.catThumb = [UIImage imageWithData:dataForCachedImage];
[dataForCachedImage release];

NSData initWithBytes length example.
Here's how I've done that before:

UInt8 j= 0x0f;
NSData *data = [[[NSData alloc] initWithBytes:&j length:sizeof(j)] autorelease];

End of NSData initWithBytes length example article.