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
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.
CFBagApplyFunction(_:_:_:) function lets you apply a function to all values in a bag.