iOS Developer Library

Developer

CoreFoundation Framework Reference CFMutableData Reference

Options
Deployment Target:

On This Page
Language:

CFMutableData Reference

CFMutableData manages dynamic binary data. The basic interface for managing binary data is provided by CFData. CFMutableData adds functions to modify the contents of a binary data object.

You create a mutable data object using either the CFDataCreateMutable or CFDataCreateMutableCopy function.

Bytes are added to a data object with the CFDataAppendBytes function. Bytes are removed from a data object with the CFDataDeleteBytes function.

CFMutableData is “toll-free bridged” with its Cocoa Foundation counterpart, NSMutableData. What this means is that the Core Foundation type is interchangeable in function or method calls with the bridged Foundation object. In other words, in a method where you see an NSMutableData * parameter, you can pass in a CFMutableDataRef, and in a function where you see a CFMutableDataRef parameter, you can pass in an NSMutableData instance. This also applies to concrete subclasses of NSMutableData. See Toll-Free Bridged Types for more information on toll-free bridging.

Functions

  • Creates an empty CFMutableData object.

    Declaration

    Swift

    func CFDataCreateMutable(_ allocator: CFAllocator!, _ capacity: CFIndex) -> CFMutableData!

    Objective-C

    CFMutableDataRef CFDataCreateMutable ( CFAllocatorRef allocator, CFIndex capacity );

    Parameters

    allocator

    The CFAllocator object to be used to allocate memory for the new object. Pass NULL or kCFAllocatorDefault to use the current default allocator.

    capacity

    The maximum number of bytes that the CFData object can contain. The CFData object starts empty and can grow to contain this number of values (and it can have less).

    Pass 0 to specify that the maximum capacity is not limited. The value must not be negative.

    Return Value

    A CFMutableData object or NULL if there was a problem creating the object. Ownership follows the Create Rule.

    Discussion

    This function creates an empty (that is, content-less) CFMutableData object. You can add raw data to this object with the CFDataAppendBytes function, and thereafter you can replace and delete characters with the appropriate CFMutableData functions. If the capacity parameter is greater than 0, any attempt to add characters beyond this limit can result in undefined behavior.

    Import Statement

    Objective-C

    @import CoreFoundation;

    Swift

    import CoreFoundation

    Availability

    Available in iOS 2.0 and later.

  • Creates a CFMutableData object by copying another CFData object.

    Declaration

    Swift

    func CFDataCreateMutableCopy(_ allocator: CFAllocator!, _ capacity: CFIndex, _ theData: CFData!) -> CFMutableData!

    Objective-C

    CFMutableDataRef CFDataCreateMutableCopy ( CFAllocatorRef allocator, CFIndex capacity, CFDataRef theData );

    Parameters

    allocator

    The CFAllocator object to be used to allocate memory for the new object. Pass NULL or kCFAllocatorDefault to use the current default allocator.

    capacity

    The maximum number of bytes that the CFData object can contain. The CFData object starts with the same length as the original object, and can grow to contain this number of bytes.

    Pass 0 to specify that the maximum capacity is not limited. If non-0, capacity must be greater than or equal to the length of theData.

    theData

    The CFData object to be copied.

    Return Value

    A CFMutableData object that has the same contents as the original object. Returns NULL if there was a problem copying the object. Ownership follows the Create Rule.

    Import Statement

    Objective-C

    @import CoreFoundation;

    Swift

    import CoreFoundation

    Availability

    Available in iOS 2.0 and later.

  • Returns a pointer to a mutable byte buffer of a CFMutableData object.

    Declaration

    Swift

    func CFDataGetMutableBytePtr(_ theData: CFMutableData!) -> UnsafeMutablePointer<UInt8>

    Objective-C

    UInt8 * CFDataGetMutableBytePtr ( CFMutableDataRef theData );

    Parameters

    theData

    A CFMutableData object. If you pass an immutable CFData object, the behavior is not defined.

    Return Value

    A pointer to the bytes associated with theData.

    Discussion

    If the length of theData’s data is not zero, this function is guaranteed to return a pointer to a CFMutableData object's internal bytes. If the length of theData’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 function result might change between different releases and on different platforms).

    Import Statement

    Objective-C

    @import CoreFoundation;

    Swift

    import CoreFoundation

    Availability

    Available in iOS 2.0 and later.

  • Appends the bytes from a byte buffer to the contents of a CFData object.

    Declaration

    Swift

    func CFDataAppendBytes(_ theData: CFMutableData!, _ bytes: UnsafePointer<UInt8>, _ length: CFIndex)

    Objective-C

    void CFDataAppendBytes ( CFMutableDataRef theData, const UInt8 *bytes, CFIndex length );

    Parameters

    theData

    A CFMutableData object. If you pass an immutable CFData object, the behavior is not defined.

    bytes

    A pointer to the buffer of bytes to be added to theData.

    length

    The number of bytes in the byte buffer bytes.

    Import Statement

    Objective-C

    @import CoreFoundation;

    Swift

    import CoreFoundation

    Availability

    Available in iOS 2.0 and later.

  • Deletes the bytes in a CFMutableData object within a specified range.

    Declaration

    Swift

    func CFDataDeleteBytes(_ theData: CFMutableData!, _ range: CFRange)

    Objective-C

    void CFDataDeleteBytes ( CFMutableDataRef theData, CFRange range );

    Parameters

    theData

    A CFMutableData object. If you pass an immutable CFData object, the behavior is not defined.

    range

    The range of bytes (that is, the starting byte and the number of bytes from that point) to delete from theData's byte buffer.

    Import Statement

    Objective-C

    @import CoreFoundation;

    Swift

    import CoreFoundation

    Availability

    Available in iOS 2.0 and later.

  • Replaces those bytes in a CFMutableData object that fall within a specified range with other bytes.

    Declaration

    Swift

    func CFDataReplaceBytes(_ theData: CFMutableData!, _ range: CFRange, _ newBytes: UnsafePointer<UInt8>, _ newLength: CFIndex)

    Objective-C

    void CFDataReplaceBytes ( CFMutableDataRef theData, CFRange range, const UInt8 *newBytes, CFIndex newLength );

    Parameters

    theData

    A CFMutableData object. If you pass an immutable CFData object, the behavior is not defined.

    range

    The range of bytes (that is, the starting byte and the number of bytes from that point) to delete from theData's byte buffer.

    newBytes

    A pointer to the buffer containing the replacement bytes.

    newLength

    The number of bytes in the byte buffer newBytes.

    Import Statement

    Objective-C

    @import CoreFoundation;

    Swift

    import CoreFoundation

    Availability

    Available in iOS 2.0 and later.

  • Increases the length of a CFMutableData object's internal byte buffer, zero-filling the extension to the buffer.

    Declaration

    Swift

    func CFDataIncreaseLength(_ theData: CFMutableData!, _ extraLength: CFIndex)

    Objective-C

    void CFDataIncreaseLength ( CFMutableDataRef theData, CFIndex extraLength );

    Parameters

    theData

    A CFMutableData object. If you pass an immutable CFData object, the behavior is not defined.

    extraLength

    The number of bytes by which to increase the byte buffer.

    Discussion

    This function increases the length of a CFMutableData object’s underlying byte buffer to a new size, initializing the new bytes to 0.

    Import Statement

    Objective-C

    @import CoreFoundation;

    Swift

    import CoreFoundation

    Availability

    Available in iOS 2.0 and later.

  • Resets the length of a CFMutableData object's internal byte buffer.

    Declaration

    Swift

    func CFDataSetLength(_ theData: CFMutableData!, _ length: CFIndex)

    Objective-C

    void CFDataSetLength ( CFMutableDataRef theData, CFIndex length );

    Parameters

    theData

    A CFMutableData object. If you pass an immutable CFData object, the behavior is not defined.

    length

    The new size of theData’s byte buffer.

    Discussion

    This function resets the length of a CFMutableData object’s underlying byte buffer to a new size. If that size is less than the current size, it truncates the excess bytes. If that size is greater than the current size, it zero-fills the extension to the byte buffer.

    Import Statement

    Objective-C

    @import CoreFoundation;

    Swift

    import CoreFoundation

    Availability

    Available in iOS 2.0 and later.

Data Types

Miscellaneous

  • A reference to a CFMutableData object.

    Declaration

    Swift

    typealias CFMutableDataRef = CFMutableData

    Objective-C

    typedef struct __CFData *CFMutableDataRef;

    Import Statement

    Objective-C

    @import CoreFoundation;

    Swift

    import CoreFoundation

    Availability

    Available in iOS 2.0 and later.