<!--
{
  "availability" : [
    "iOS: -",
    "iPadOS: -",
    "macCatalyst: -",
    "macOS: -",
    "tvOS: -",
    "visionOS: -",
    "watchOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "CoreFoundation",
  "identifier" : "/documentation/CoreFoundation/CFBag",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Core Foundation"
    ],
    "preciseIdentifier" : "c:@T@CFBagRef"
  },
  "title" : "CFBag"
}
-->

# CFBag

```
class CFBag
```

## Overview

CFBag and its derived mutable type, [`CFMutableBag`](/documentation/CoreFoundation/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(_:_:_:_:)`](/documentation/CoreFoundation/CFBagCreate(_:_:_:_:)) or [`CFBagCreateCopy(_:_:)`](/documentation/CoreFoundation/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(_:)`](/documentation/CoreFoundation/CFBagGetCount(_:)) returns the number of values in a bag, the [`CFBagContainsValue(_:_:)`](/documentation/CoreFoundation/CFBagContainsValue(_:_:)) function checks if a value is in a bag, and [`CFBagGetValues(_:_:)`](/documentation/CoreFoundation/CFBagGetValues(_:_:)) returns a C array containing all the values in a bag.

The [`CFBagApplyFunction(_:_:_:)`](/documentation/CoreFoundation/CFBagApplyFunction(_:_:_:)) function lets you apply a function to all values in a bag.

## Topics

### Creating a Bag

[`CFBagCreate(_:_:_:_:)`](/documentation/CoreFoundation/CFBagCreate(_:_:_:_:))

Creates an immutable bag containing specified values.

[`CFBagCreateCopy(_:_:)`](/documentation/CoreFoundation/CFBagCreateCopy(_:_:))

Creates an immutable bag with the values of another bag.

### Examining a Bag

[`CFBagContainsValue(_:_:)`](/documentation/CoreFoundation/CFBagContainsValue(_:_:))

Reports whether or not a value is in a bag.

[`CFBagGetCount(_:)`](/documentation/CoreFoundation/CFBagGetCount(_:))

Returns the number of values currently in a bag.

[`CFBagGetCountOfValue(_:_:)`](/documentation/CoreFoundation/CFBagGetCountOfValue(_:_:))

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

[`CFBagGetValue(_:_:)`](/documentation/CoreFoundation/CFBagGetValue(_:_:))

Returns a requested value from a bag.

[`CFBagGetValueIfPresent(_:_:_:)`](/documentation/CoreFoundation/CFBagGetValueIfPresent(_:_:_:))

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

[`CFBagGetValues(_:_:)`](/documentation/CoreFoundation/CFBagGetValues(_:_:))

Fills a buffer with values from a bag.

### Applying a Function to the Contents of a Bag

[`CFBagApplyFunction(_:_:_:)`](/documentation/CoreFoundation/CFBagApplyFunction(_:_:_:))

Calls a function once for each value in a bag.

### Getting the CFBag Type ID

[`CFBagGetTypeID()`](/documentation/CoreFoundation/CFBagGetTypeID())

Returns the type identifier for the CFBag opaque type.

### Callbacks

[`CFBagApplierFunction`](/documentation/CoreFoundation/CFBagApplierFunction)

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

[`CFBagCopyDescriptionCallBack`](/documentation/CoreFoundation/CFBagCopyDescriptionCallBack)

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

[`CFBagEqualCallBack`](/documentation/CoreFoundation/CFBagEqualCallBack)

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

[`CFBagHashCallBack`](/documentation/CoreFoundation/CFBagHashCallBack)

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.

[`CFBagReleaseCallBack`](/documentation/CoreFoundation/CFBagReleaseCallBack)

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

[`CFBagRetainCallBack`](/documentation/CoreFoundation/CFBagRetainCallBack)

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

### Data Types

[`CFBagCallBacks`](/documentation/CoreFoundation/CFBagCallBacks)

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

### Constants

[Predefined Callback Structures](/documentation/CoreFoundation/cfbag-predefined-callback-structures)

CFBag provides some predefined callbacks for your convenience.

## See Also

  [Collections Programming Topics for Core Foundation](https://developer.apple.com/library/archive/documentation/CoreFoundation/Conceptual/CFCollections/CFCollections.html#//apple_ref/doc/uid/10000124i)



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)