Error:

Create new empty data object

NSData *data = [NSData data];

Create new data object with buffer

const void *buffer;
// allocate and initialize buffer
NSData * data = [NSData dataWithBytes:buffer length:len];
// buffer can be free-ed now

Create new data object with buffer and take over ownership

const void *buffer;
// allocate buffer using malloc 
NSData * data = [NSData dataWithBytesNoCopy:buffer length:len];
// buffer will be free-ed when data object is deallocated

Create new data object with buffer and do not pass ownership

const void *buffer;
// allocate buffer using malloc 
NSData * data = [NSData dataWithBytesNoCopy:buffer length:len freeWhenDone:NO];
// buffer will not be free-ed when data object is deallocated

Read file to NSData

NSData *contents = [NSData dataWithContentsOfFile:@"/path/to/file"];

Read file to NSData and handle errors

NSError *err;
NSData *contents = [NSData dataWithContentsOfFile:@"/path/to/file" options:0 error:&err];
if (contents == nil)
    NSLog(@"Error reading file: %@", err.localizedDescription);

Read URL to NSData

NSURL *url = [NSURL fileURLWithPath:@"/path/to/file"];
NSData *contents = [NSData dataWithContentsOfURL:url];

Read URL to NSData and handle errors

NSError *err;
NSURL *url = [NSURL fileURLWithPath:@"/path/to/file"];
NSData *contents = [NSData dataWithContentsOfURL:url options:0 error:&err];
if (contents == nil)
    NSLog(@"Error reading file: %@", err.localizedDescription);

Encode NSData using base64 encoding (pre-10.9)

NSString *base64 = [data performSelector:@selector(base64Encoding)]

Encode NSData using base64 encoding (10.9)

NSString *base64str = [data base64EncodedStringWithOptions:0];

Decode base64-encoded string to NSData (10.9)

NSString *base64str = @"dGVzdAo=";
NSData *data = [[NSData alloc] initWithBase64EncodedString:base64str options:0];

Decode base64-encoded data to NSData (10.9)

NSString *base64data = [NSData dataWithContentsOfFile:@"/path/to/base64.txt"];
NSData *data = [[NSData alloc] initWithBase64EncodedData:base64data options:0];

Get pointer to bytes of NSData

// read-only buffer
const void *ptr = [data bytes];

Copy bytes from the beginning of NSData

unsigned char *bytes = (unsigned char*)malloc(BUFFER_LEN);
[data getBytes:bytes length:BUFFER_LEN];

Copy the range of bytes from the data

unsigned char *bytes = (unsigned char*)malloc(BUFFER_LEN);
[data getBytes:bytes range:NSMakeRange(100, BUFFER_LEN)];

Copy the range of bytes from the data as NSData

NSData *subdata = [data subdataWithRange:NSMakeRange(0, 10)];

Find occurrence of NSData in the data

NSRange result = [data rangeOfData:dataToFind options:0 range:NSMakeRange(0, [data length])];

Get description of NSData in property list format

NSString *plist = [data description];

Check if two data objects are equal

BOOL eq = [data isEqualToData:otherData];

Get length of data in bytes

NSUInteger total = [data length];

Save the data to file

[data writeToFile:@"/path/to/file" atomically:YES];

Save the data to URL

NSURL *url = [NSURL fileURLWithPath:@"/path/to/file"];
[data writeToURL:url atomically:YES];

Create mutable copy of the data

NSMutableData *md = [data mutableCopy];