Inherits from | |
Conforms to | |
Framework | /System/Library/Frameworks/Foundation.framework |
Availability | Available in Mac OS X v10.0 and later. |
Companion guide | |
Declared in | NSData.h |
Related sample code |
NSMutableData
(and its superclass NSData
) provide data objects, object-oriented wrappers for byte buffers. Data objects let simple allocated buffers (that is, data with no embedded pointers) take on the behavior of Foundation objects. They are typically used for data storage and are also useful in Distributed Objects applications, where data contained in data objects can be copied or moved between applications. NSData
creates static data objects, and NSMutableData
creates dynamic data objects. You can easily convert one type of data object to the other with the initializer that takes an NSData
object or an NSMutableData
object as an argument.
NSMutableData
is “toll-free bridged” with its Core Foundation counterpart, CFData
. This means that the Core Foundation type is interchangeable in function or method calls with the bridged Foundation object. Therefore, in a method where you see an NSMutableData *
parameter, you can pass a CFDataRef
, and in a function where you see a CFDataRef
parameter, you can pass an NSMutableData
instance (you cast one type to the other to suppress compiler warnings). See Interchangeable Data Types for more information on toll-free bridging.
– replaceBytesInRange:withBytes:
– replaceBytesInRange:withBytes:length:
– resetBytesInRange:
– setData:
Creates and returns an NSMutableData
object capable of holding the specified number of bytes.
+ (id)dataWithCapacity:(NSUInteger)aNumItems
The number of bytes the new data object can initially contain.
A new NSMutableData
object capable of holding aNumItems bytes.
This method doesn’t necessarily allocate the requested memory right away. Mutable data objects allocate additional memory as needed, so aNumItems simply establishes the object’s initial capacity. When it does allocate the initial memory, though, it allocates the specified amount. This method sets the length of the data object to 0
.
If the capacity specified in aNumItems is greater than four memory pages in size, this method may round the amount of requested memory up to the nearest full page.
NSData.h
Creates and returns an NSMutableData
object containing a given number of zeroed bytes.
+ (id)dataWithLength:(NSUInteger)length
The number of bytes the new data object initially contains.
A new NSMutableData
object of length bytes, filled with zeros.
NSData.h
Appends to the receiver a given number of bytes from a given buffer.
- (void)appendBytes:(const void *)bytes length:(NSUInteger)length
A buffer containing data to append to the receiver's content.
The number of bytes from bytes to append.
A sample using this method can be found in Working With Mutable Binary Data.
NSData.h
Appends the content of another NSData
object to the receiver.
- (void)appendData:(NSData *)otherData
The data object whose content is to be appended to the contents of the receiver.
NSData.h
Increases the length of the receiver by a given number of bytes.
- (void)increaseLengthBy:(NSUInteger)extraLength
The number of bytes by which to increase the receiver's length.
The additional bytes are all set to 0
.
NSData.h
Returns an initialized NSMutableData
object capable of holding the specified number of bytes.
- (id)initWithCapacity:(NSUInteger)capacity
The number of bytes the data object can initially contain.
An initialized NSMutableData
object capable of holding capacity bytes.
This method doesn’t necessarily allocate the requested memory right away. Mutable data objects allocate additional memory as needed, so aNumItems simply establishes the object’s initial capacity. When it does allocate the initial memory, though, it allocates the specified amount. This method sets the length of the data object to 0
.
If the capacity specified in aNumItems is greater than four memory pages in size, this method may round the amount of requested memory up to the nearest full page.
NSData.h
Initializes and returns an NSMutableData
object containing a given number of zeroed bytes.
- (id)initWithLength:(NSUInteger)length
The number of bytes the object initially contains.
An initialized NSMutableData
object containing length zeroed bytes.
NSData.h
Returns a pointer to the receiver’s data.
- (void *)mutableBytes
A pointer to the receiver’s data.
If the length of the receiver’s data is not zero, this function is guaranteed to return a pointer to the object's internal bytes. If the length of receiver’s data is zero, this function may or may not return NULL
dependent upon many factors related to how the object was created (moreover, in this case the method result might change between different releases).
A sample using this method can be found in Working With Mutable Binary Data.
NSData.h
Replaces with a given set of bytes a given range within the contents of the receiver.
- (void)replaceBytesInRange:(NSRange)range withBytes:(const void *)bytes
The range within the receiver's contents to replace with bytes
. The range must not exceed the bounds of the receiver.
The data to insert into the receiver's contents.
If the location of range isn’t within the receiver’s range of bytes, an NSRangeException
is raised. The receiver is resized to accommodate the new bytes, if necessary.
A sample using this method is given in Working With Mutable Binary Data.
NSData.h
Replaces with a given set of bytes a given range within the contents of the receiver.
- (void)replaceBytesInRange:(NSRange)range withBytes:(const void *)replacementBytes length:(NSUInteger)replacementLength
The range within the receiver's contents to replace with bytes
. The range must not exceed the bounds of the receiver.
The data to insert into the receiver's contents.
The number of bytes to take from replacementBytes.
If the length of range is not equal to replacementLength, the receiver is resized to accommodate the new bytes. Any bytes past range in the receiver are shifted to accommodate the new bytes. You can therefore pass NULL
for replacementBytes and 0
for replacementLength to delete bytes in the receiver in the range range. You can also replace a range (which might be zero-length) with more bytes than the length of the range, which has the effect of insertion (or “replace some and insert more”).
NSData.h
Replaces with zeroes the contents of the receiver in a given range.
- (void)resetBytesInRange:(NSRange)range
The range within the contents of the receiver to be replaced by zeros. The range must not exceed the bounds of the receiver.
If the location of range isn’t within the receiver’s range of bytes, an NSRangeException
is raised. The receiver is resized to accommodate the new bytes, if necessary.
NSData.h
Replaces the entire contents of the receiver with the contents of another data object.
- (void)setData:(NSData *)aData
The data object whose content replaces that of the receiver.
As part of its implementation, this method calls replaceBytesInRange:withBytes:
.
NSData.h
Extends or truncates a mutable data object to a given length.
- (void)setLength:(NSUInteger)length
The new length for the receiver.
If the mutable data object is extended, the additional bytes are filled with zeros.
NSData.h
© 2007 Apple Inc. All Rights Reserved. (Last updated: 2007-03-26)