CFBag and its derived mutable type, CFMutableBag, manage non-sequential collections of values called bags in which there can be duplicate values. CFBag creates static bags and CFMutableBag creates dynamic bags.

Use bags or sets as an alternative to arrays when the order of elements isn't important and performance in testing whether a value is contained in the collection is a consideration—while arrays are ordered, testing for membership is slower than with bags or sets. Use bags over sets if you want to allow duplicate values in your collections.

You create a static bag object using either the CFBagCreate or CFBagCreateCopy function. These functions return a bag containing the values you pass in as arguments. (Note that bags can't contain NULL pointers; in most cases, though, you can use the kCFNull constant instead.) Values are not copied but retained using the retain callback provided when the bag was created. Similarly, when a value is removed from a bag, it is released using the release callback.

CFBag provides functions for querying the values of a bag. The CFBagGetCount returns the number of values in a bag, the CFBagContainsValue function checks if a value is in a bag, and CFBagGetValues returns a C array containing all the values in a bag.

The CFBagApplyFunction function lets you apply a function to all values in a bag.


Creating a Bag


Creates an immutable bag containing specified values.


Creates an immutable bag with the values of another bag.

Examining a Bag


Reports whether or not a value is in a bag.


Returns the number of values currently in a bag.


Returns the number of times a value occurs in a bag.


Returns a requested value from a bag.


Reports whether or not a value is in a bag, and returns that value indirectly if it exists.


Fills a buffer with values from a bag.

Applying a Function to the Contents of a Bag


Calls a function once for each value in a bag.

Getting the CFBag Type ID


Returns the type identifier for the CFBag opaque type.



Prototype of a callback function that may be applied to every value in a bag.


Prototype of a callback function used to get a description of a value in a bag.


Prototype of a callback function used to determine if two values in a bag are equal.


Prototype of a callback function invoked to compute a hash code for a value. Hash codes are used when values are accessed, added, or removed from a collection.


Prototype of a callback function used to release a value before it’s removed from a bag.


Prototype of a callback function used to retain a value being added to a bag.

Data Types


This structure contains the callbacks used to retain, release, describe, and compare the values of a CFBag object.


A reference to an immutable bag object.


Predefined Callback Structures

CFBag provides some predefined callbacks for your convenience.

See Also

Opaque Types


The CFFileDescriptor provides an opaque type to monitor file descriptors for read and write activity via CFRunLoop.


CFStringTokenizer allows you to tokenize strings into words, sentences or paragraphs in a language-neutral way. It supports languages such as Japanese and Chinese that do not delimit words by spaces, as well as de-compounding German compounds. You can obtain Latin transcription for tokens. It also provides language identification API.