CFData and its derived mutable type, CFMutableData, provide support for 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 Core Foundation objects. CFData creates static data objects, and CFMutableData creates dynamic data objects. Data objects are typically used for raw data storage.

You use the CFData​Create(_:​_:​_:​) and CFData​Create​Copy(_:​_:​) functions to create static data objects. These functions make a new copy of the supplied data. To create a data object that uses the supplied buffer instead of making a separate copy, use the CFData​Create​With​Bytes​No​Copy(_:​_:​_:​_:​) function. You use the CFData​Get​Bytes(_:​_:​_:​) function to retrieve the bytes and the CFData​Get​Length(_:​) function to get the length of the bytes.

CFData is “toll-free bridged” with its Cocoa Foundation counterpart, NSData. 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 NSData * parameter, you can pass in a CFData​Ref, and in a function where you see a CFData​Ref parameter, you can pass in an NSData instance. This also applies to concrete subclasses of NSData. See Toll-Free Bridged Types for more information on toll-free bridging.


Creating a CFData Object

func CFData​Create(CFAllocator!, Unsafe​Pointer<UInt8>!, CFIndex)

Creates an immutable CFData object using data copied from a specified byte buffer.

func CFData​Create​Copy(CFAllocator!, CFData!)

Creates an immutable copy of a CFData object.

func CFData​Create​With​Bytes​No​Copy(CFAllocator!, Unsafe​Pointer<UInt8>!, CFIndex, CFAllocator!)

Creates an immutable CFData object from an external (client-owned) byte buffer.

Examining a CFData Object

func CFData​Get​Byte​Ptr(CFData!)

Returns a read-only pointer to the bytes of a CFData object.

func CFData​Get​Bytes(CFData!, CFRange, Unsafe​Mutable​Pointer<UInt8>!)

Copies the byte contents of a CFData object to an external buffer.

func CFData​Get​Length(CFData!)

Returns the number of bytes contained by a CFData object.

func CFData​Find(CFData!, CFData!, CFRange, CFData​Search​Flags)

Finds and returns the range within a data object of the first occurrence of the given data, within a given range, subject to any given options.

Getting the CFData Type ID

func CFData​Get​Type​ID()

Returns the type identifier for the CFData opaque type.

Data Types


A reference to an immutable CFData object.


A CFOption​Flags type for specifying options for searching.