Class: NSMutableData

Inherits:
NSData show all

Overview

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.

Direct Known Subclasses

NSPurgeableData

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from NSData

#bytes, data, dataWithBytes:length:, dataWithBytesNoCopy:length:, dataWithBytesNoCopy:length:freeWhenDone:, dataWithContentsOfFile:, dataWithContentsOfFile:options:error:, dataWithContentsOfURL:, dataWithContentsOfURL:options:error:, dataWithData:, #description, #getBytes:length:, #getBytes:range:, #initWithBytes:length:, #initWithBytesNoCopy:length:, #initWithBytesNoCopy:length:freeWhenDone:, #initWithContentsOfFile:, #initWithContentsOfFile:options:error:, #initWithContentsOfURL:, #initWithContentsOfURL:options:error:, #initWithData:, #isEqualToData:, #length, #rangeOfData:options:range:, #subdataWithRange:, #to_str, #writeToFile:atomically:, #writeToFile:options:error:, #writeToURL:atomically:, #writeToURL:options:error:

Methods inherited from NSObject

#!, #!=, #!~, #, #==, #===, #=~, #Rational, #__callee__, #__method__, #__send__, #__type__, `, alloc, allocWithZone:, #autoContentAccessingProxy, autoload, autoload?, autorelease_pool, #awakeAfterUsingCoder:, binding, block_given?, caller, cancelPreviousPerformRequestsWithTarget:, cancelPreviousPerformRequestsWithTarget:selector:object:, catch, class, classFallbacksForKeyedArchiver, #classForCoder, #classForKeyedArchiver, classForKeyedUnarchiver, #clone, conformsToProtocol:, #copy, copyWithZone:, #dealloc, #define_singleton_method, description, display, #doesNotRecognizeSelector:, #dup, #enum_for, #eql?, #equal?, #extend, fail, #finalize, format, #forwardInvocation:, #forwardingTargetForSelector:, framework, #freeze, #frozen?, getpass, gets, global_variables, #init, initialize, #initialize_clone, #initialize_copy, #initialize_dup, #inspect, instanceMethodForSelector:, instanceMethodSignatureForSelector:, #instance_eval, #instance_exec, #instance_of?, #instance_variable_defined?, #instance_variable_get, #instance_variable_set, #instance_variables, instancesRespondToSelector:, isSubclassOfClass:, #is_a?, iterator?, #kind_of?, lambda, load, load_bridge_support_file, load_plist, local_variables, loop, #method, #methodForSelector:, #methodSignatureForSelector:, #methods, #mutableCopy, mutableCopyWithZone:, new, #nil?, open, p, #performSelector:onThread:withObject:waitUntilDone:, #performSelector:onThread:withObject:waitUntilDone:modes:, #performSelector:withObject:afterDelay:, #performSelector:withObject:afterDelay:inModes:, #performSelectorInBackground:withObject:, #performSelectorOnMainThread:withObject:waitUntilDone:, #performSelectorOnMainThread:withObject:waitUntilDone:modes:, print, printf, #private_methods, proc, #protected_methods, #public_method, #public_methods, #public_send, putc, puts, raise, rand, readline, readlines, #replacementObjectForCoder:, #replacementObjectForKeyedArchiver:, require, resolveClassMethod:, resolveInstanceMethod:, #respond_to?, #respond_to_missing?, select, #send, setVersion:, #singleton_methods, sprintf, srand, superclass, #taint, #tainted?, #tap, test, throw, #to_plist, #to_s, trace_var, trap, #trust, #untaint, untrace_var, #untrust, #untrusted?, version

Constructor Details

This class inherits a constructor from NSObject

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class NSObject

Class Method Details

+ (Object) dataWithCapacity(aNumItems)

Creates and returns an NSMutableData object capable of holding the specified number of 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.

Parameters:

  • aNumItems (Integer)

    The number of bytes the new data object can initially contain.

Returns:

+ (Object) dataWithLength(length)

Creates and returns an NSMutableData object containing a given number of zeroed bytes.

Parameters:

  • length (Integer)

    The number of bytes the new data object initially contains.

Returns:

Instance Method Details

- (Object) appendBytes(bytes, length:length)

Appends to the receiver a given number of bytes from a given buffer. A sample using this method can be found in Working With Mutable Binary Data.

Parameters:

  • bytes (Object)

    A buffer containing data to append to the receiver's content.

  • length (Integer)

    The number of bytes from bytes to append.

Returns:

- (Object) appendData(otherData)

Appends the content of another NSData object to the receiver.

Parameters:

  • otherData (NSData)

    The data object whose content is to be appended to the contents of the receiver.

Returns:

- (Object) increaseLengthBy(extraLength)

Increases the length of the receiver by a given number of bytes. The additional bytes are all set to 0.

Parameters:

  • extraLength (Integer)

    The number of bytes by which to increase the receiver's length.

Returns:

- (Object) initWithCapacity(capacity)

Returns an initialized NSMutableData object capable of holding the specified number of 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.

Parameters:

  • capacity (Integer)

    The number of bytes the data object can initially contain.

Returns:

- (Object) initWithLength(length)

Initializes and returns an NSMutableData object containing a given number of zeroed bytes.

Parameters:

  • length (Integer)

    The number of bytes the object initially contains.

Returns:

- (Object) mutableBytes

Returns 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.

Returns:

  • (Object)

    A pointer to the receiver’s data.

- (Object) replaceBytesInRange(range, withBytes:bytes)

Replaces with a given set of bytes a given range within the contents 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. A sample using this method is given in Working With Mutable Binary Data.

Parameters:

  • range (NSRange)

    The range within the receiver’s contents to replace with bytes. The range must not exceed the bounds of the receiver.

  • bytes (Object)

    The data to insert into the receiver’s contents.

Returns:

- (Object) replaceBytesInRange(range, withBytes:replacementBytes, length:replacementLength)

Replaces with a given set of bytes a given range within the contents of the receiver. 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”).

Parameters:

  • range (NSRange)

    The range within the receiver’s contents to replace with bytes. The range must not exceed the bounds of the receiver.

  • replacementBytes (Object)

    The data to insert into the receiver’s contents.

  • replacementLength (Integer)

    The number of bytes to take from replacementBytes.

Returns:

- (Object) resetBytesInRange(range)

Replaces with zeroes the contents of the receiver in a given range. 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.

Parameters:

  • range (NSRange)

    The range within the contents of the receiver to be replaced by zeros. The range must not exceed the bounds of the receiver.

Returns:

- (Object) setData(aData)

Replaces the entire contents of the receiver with the contents of another data object. As part of its implementation, this method calls replaceBytesInRange:withBytes:.

Parameters:

  • aData (NSData)

    The data object whose content replaces that of the receiver.

Returns:

- (Object) setLength(length)

Extends or truncates a mutable data object to a given length. If the mutable data object is extended, the additional bytes are filled with zeros.

Parameters:

  • length (Integer)

    The new length for the receiver.

Returns: