O'Reilly logo

Cocoa in a Nutshell by James Duncan Davidson, Michael Beam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 *) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required