Name

NSData — Mac OS X 10.0

Synopsis

This class is used to store immutable data as a string of bytes. In essence, NSData is an Objective-C wrapper around a C data buffer. To access the buffer directly, use the bytes method, which returns a pointer to the buffer. The number of bytes contained in the buffer is found by invoking the length method. Additionally, NSData provides a handful of methods for copy bytes from the data object into a buffer. These methods include getBytes:, getBytes:length:, and getBytes:range:. All three of these methods take in the first parameter a generic C pointer to the buffer in which the bytes are copied.

NSData is toll-free bridged with the Core Foundation type CFData. As such, NSData objects can be used interchangeably with the CFData pointer type, CFDataRef.

Note that NSData is an immutable class, which means the contents of the data object cannot be changed after initialization.

image with no caption

@interface NSData : NSObject <NSCoding, NSCopying, NSMutableCopying>
                                  // Convenience Constructors
   + (id)data;
   + (id)dataWithBytes:(const void *)bytes 
                                 length:(unsigned)length;
   + (id)dataWithBytesNoCopy:(void *)bytes 
                                 length:(unsigned)length;
   + (id)dataWithBytesNoCopy:(void *)bytes 
                                 length:(unsigned)length 
                                 freeWhenDone:(BOOL)b;
   + (id)dataWithContentsOfFile:(NSString *)path;
   + (id)dataWithContentsOfMappedFile:(NSString *)path;
   + (id)dataWithContentsOfURL:(NSURL *) ...

Get Cocoa in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.