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

# CFSet

```
class CFSet
```

## Overview

CFSet and its derived mutable type, [`CFMutableSet`](/documentation/CoreFoundation/CFMutableSet), provide support for the mathematical concept of a set. A set, both in its mathematical sense and in the implementation of CFSet, is an unordered collection of distinct elements. CFSet creates static sets and CFMutableSet creates dynamic sets.

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 set object using either the [`CFSetCreate(_:_:_:_:)`](/documentation/CoreFoundation/CFSetCreate(_:_:_:_:)) or [`CFSetCreateCopy(_:_:)`](/documentation/CoreFoundation/CFSetCreateCopy(_:_:)) function. These functions return a set containing the values you pass in as arguments. (Note that sets can’t contain `NULL` pointers; in most cases, though, you can use the [`kCFNull`](/documentation/CoreFoundation/kCFNull) constant instead.) Values are not copied but retained using the retain callback provided when the set was created. Similarly, when a value is removed from a set, it is released using the release callback.

CFSet provides functions for querying the values of a set. The [`CFSetGetCount(_:)`](/documentation/CoreFoundation/CFSetGetCount(_:)) returns the number of values in a set, the [`CFSetContainsValue(_:_:)`](/documentation/CoreFoundation/CFSetContainsValue(_:_:)) function checks if a value is in a set, and [`CFSetGetValues(_:_:)`](/documentation/CoreFoundation/CFSetGetValues(_:_:)) returns a C array containing all the values in a set.

CFSet is “toll-free bridged” with its Cocoa Foundation counterpart, <doc://com.apple.documentation/documentation/Foundation/NSSet>. This means that the Core Foundation type is interchangeable in function or method calls with the bridged Foundation object. Therefore, in a method where you see an `NSSet *` parameter, you can pass in a `CFSetRef`, and in a function where you see a `CFSetRef` parameter, you can pass in an NSSet instance. This also applies to concrete subclasses of NSSet. See [Toll-Free Bridged Types](https://developer.apple.com/library/archive/documentation/CoreFoundation/Conceptual/CFDesignConcepts/Articles/tollFreeBridgedTypes.html#//apple_ref/doc/uid/TP40010677) for more information on toll-free bridging.

## Topics

### Creating Sets

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

Creates an immutable CFSet object containing supplied values.

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

Creates an immutable set containing the values of an existing set.

### Examining a Set

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

Returns a Boolean that indicates whether a set contains a given value.

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

Returns the number of values currently in a set.

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

Returns the number of values in a set that match a given value.

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

Obtains a specified value from a set.

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

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

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

Obtains all values in a set.

### Applying a Function to Set Members

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

Calls a function once for each value in a set.

### Getting the CFSet Type ID

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

Returns the type identifier for the CFSet type.

### Callbacks

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

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

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

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

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

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

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

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

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

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

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

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

### Data Types

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

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

### Constants

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

CFSet 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)