< Previous PageNext Page > Hide TOC

Data Objects

Data objects are object-oriented wrappers for byte buffers. In these data objects, simple allocated buffers (that is, data with no embedded pointers) take on the behavior of other objects—that is, they encapsulate data and provide operations to manipulate that data. Data objects are typically used to store data. They are also useful in internet and intranet applications because the data contained in data objects can be copied or moved between applications.

Important:  Data objects are available to Cocoa and Carbon developers. The Cocoa Foundation classes, NSData and NSMutableData, are “toll-free bridged” with their Core Foundation counterparts, CFData (see CFData Reference) and CFMutableData (see CFMutableData Reference). This means that the Core Foundation opaque type is interchangeable in function or method calls with the bridged Foundation object. In other words, in an API having an NSData * parameter, you can pass in a CFDataRef, and in an API having a CFDataRef parameter, you can pass in an NSData instance. You cannot, however, pass an NSData object to an API that expects a mutable CFData reference; you must use an NSMutableData object instead. This document refers to these objects as simply data objects or mutable data objects for objects that can be changed after creation.

The size of the data that an instance of NSData or NSMutableData can wrap is subject to platform-dependent limitations—see NSData. When the data size is more than a few memory pages, the object uses virtual memory management. A data object can also wrap preexisting data, regardless of how the data was allocated. The object contains no information about the data itself (such as its type); the responsibility for deciding how to use the data lies with the client. In particular, it will not handle byte-order swapping when distributed between big-endian and little-endian machines. (In Cocoa, use NSValue for typed data.)

Data objects provide an operating system–independent way to benefit from copy-on-write memory. The copy-on-write technique means that when data is copied through a virtual memory copy, an actual copy of the data is not made until there is an attempt to modify it.

Typically, you specify the bytes and the length of the bytes stored in a data object when creating that object. You can also extract bytes of a given range from a data object, compare data stored in two data objects, and write data to a URL. You use mutable data objects when you need to modify the data after creation. You can truncate, extend the length of, append data to, and replace a range of bytes in a mutable data object.



< Previous PageNext Page > Hide TOC


© 2003, 2009 Apple Inc. All Rights Reserved. (Last updated: 2009-05-06)


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.