Return to the Alphabetic Index
Return to the Class Browser
Return to the Picture Browser
Copyright (c) 1994 by NeXT Computer, Inc. All Rights Reserved.

NSData

Inherits From: NSObject

Conforms To: NSCoding, NSCopying, NSMutableCopying NSObject (NSObject)

Declared In: Foundation/NSData.h

Class Description

The NSData class declares the programmatic interface to objects that contain data in the form of bytes. NSData objects hold a static collection of bytes; NSData's subclass, NSMutableData, defines objects that hold modifiable data. These two classes provide an object-oriented approach to memory allocation, a facility that in procedural programming is accessed through functions like malloc(). Furthermore, these classes take advantage of operating system primitives when allocating large blocks of memory.

NSData's two primitive methodsbytes and lengthprovide the basis for all the other methods in its interface. The bytes method returns a pointer to the bytes contained in the data object. length returns the number of bytes contained in the data object.

NSData and NSMutableData objects are commonly used to hold the contents of a file. The methods dataWithContentsOfFile: and dataWithContentsOfMappedFile: return objects that represent a file's contents. The writeToFile:atomically: method enables you to write the contents of a data object to a file.

NSData provides access methods for copying bytes from a data object into a buffer. Use getBytes: to copy the entire contents of the object or getBytes:length: to copy a subset, starting with the first byte. getBytes:range: copies a range of bytes from a starting point within the bytes themselves. You can also return a data object that contains a subset of the bytes in another data object by using the subdataWithRange: method. Or, you can use the description method to return an NSString representation of the bytes in a data object.

For determining if two data objects are equal, NSData provides the isEqualToData: method, which does a byte-for-byte comparison.

Allocating and Initializing an NSData Object

Accessing Data

Querying a Data Object

Storing Data

Deserializing Data