<!--
{
  "availability" : [
    "iOS: 2.0.0 -",
    "iPadOS: 2.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.0.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSSet",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSSet"
  },
  "title" : "NSSet"
}
-->

# NSSet

A static, unordered collection of unique objects.

```
class NSSet
```

## Overview

The [`NSSet`](/documentation/Foundation/NSSet), [`NSMutableSet`](/documentation/Foundation/NSMutableSet), and [`NSCountedSet`](/documentation/Foundation/NSCountedSet) classes declare the programmatic interface to an unordered collection of objects.

[`NSSet`](/documentation/Foundation/NSSet) declares the programmatic interface for static sets of distinct objects. You establish a static set’s entries when it’s created, and can’t modify the entries after that. [`NSMutableSet`](/documentation/Foundation/NSMutableSet), on the other hand, declares a programmatic interface for dynamic sets of distinct objects. A dynamic — or mutable — set allows the addition and deletion of entries at any time, automatically allocating memory as needed.

Use sets as an alternative to arrays when the order of elements isn’t important and you need to consider performance in testing whether the set contains an object. With an array, testing for membership is slower than with sets.

[`NSSet`](/documentation/Foundation/NSSet) is “toll-free bridged” with its Core Foundation counterpart, <doc://com.apple.documentation/documentation/CoreFoundation/CFSet>. See [Toll-Free Bridging](https://developer.apple.com/library/archive/documentation/General/Conceptual/CocoaEncyclopedia/Toll-FreeBridgin/Toll-FreeBridgin.html#//apple_ref/doc/uid/TP40010810-CH2) for more information on toll-free bridging.

In Swift, use this class instead of a <doc://com.apple.documentation/documentation/Swift/Set> constant in cases where you require reference semantics.

### Subclassing Notes

There should be little need of subclassing. If you need to customize behavior, it’s often better to consider composition instead of subclassing.

#### Methods to Override

In a subclass, you must override all of its primitive methods:

- [`count`](/documentation/Foundation/NSSet/count)
- [`member(_:)`](/documentation/Foundation/NSSet/member(_:))
- [`objectEnumerator()`](/documentation/Foundation/NSSet/objectEnumerator())

#### Alternatives to Subclassing

Before making a custom class of [`NSSet`](/documentation/Foundation/NSSet), investigate [`NSHashTable`](/documentation/Foundation/NSHashTable) and the corresponding Core Foundation type, <doc://com.apple.documentation/documentation/CoreFoundation/CFSet>. Because [`NSSet`](/documentation/Foundation/NSSet) and <doc://com.apple.documentation/documentation/CoreFoundation/CFSet> are “toll-free bridged,” you can substitute a <doc://com.apple.documentation/documentation/CoreFoundation/CFSet> object for a [`NSSet`](/documentation/Foundation/NSSet) object in your code (with appropriate casting). Although they’re corresponding types, <doc://com.apple.documentation/documentation/CoreFoundation/CFSet> and [`NSSet`](/documentation/Foundation/NSSet) don’t have identical interfaces or implementations, and you can sometimes do things with <doc://com.apple.documentation/documentation/CoreFoundation/CFSet> that you can’t easily do with [`NSSet`](/documentation/Foundation/NSSet).

If the behavior you want to add supplements that of the existing class, you could write a category on [`NSSet`](/documentation/Foundation/NSSet). Keep in mind, however, that this category affects all instances of [`NSSet`](/documentation/Foundation/NSSet) that you use, and this might have unintended consequences. Alternatively, you could use composition to achieve the desired behavior.

## Topics

### Creating a Set

[`+  set`](/documentation/Foundation/NSSet/set)

Creates and returns an empty set.

[`+  setWithArray:`](/documentation/Foundation/NSSet/setWithArray:)

Creates and returns a set containing a uniqued collection of the objects contained in a given array.

[`+  setWithObject:`](/documentation/Foundation/NSSet/init(object:))

Creates and returns a set that contains a single given object.

[`+  setWithObjects:`](/documentation/Foundation/NSSet/setWithObjects:)

Creates and returns a set containing the objects in a given argument list.

[`+  setWithObjects:count:`](/documentation/Foundation/NSSet/init(objects:count:)-65ni4)

Creates and returns a set containing a specified number of objects from a given C array of objects.

[`+  setWithSet:`](/documentation/Foundation/NSSet/setWithSet:)

Creates and returns a set containing the objects from another set.

[`-  setByAddingObject:`](/documentation/Foundation/NSSet/adding(_:))

Returns a new set formed by adding a given object to the receiving set.

[`-  setByAddingObjectsFromSet:`](/documentation/Foundation/NSSet/addingObjects(from:)-2i31h)

Returns a new set formed by adding the objects in a given set to the receiving set.

[`-  setByAddingObjectsFromArray:`](/documentation/Foundation/NSSet/addingObjects(from:)-544m9)

Returns a new set formed by adding the objects in a given array to the receiving set.

### Initializing a Set

[`-  initWithArray:`](/documentation/Foundation/NSSet/init(array:))

Initializes a newly allocated set with the objects that are contained in a given array.

[`-  initWithObjects:`](/documentation/Foundation/NSSet/initWithObjects:)

Initializes a newly allocated set with members taken from the specified list of objects.

[`-  initWithObjects:count:`](/documentation/Foundation/NSSet/init(objects:count:)-7kift)

Initializes a newly allocated set with a specified number of objects from a given C array of objects.

[`-  initWithSet:`](/documentation/Foundation/NSSet/init(set:)-1xovx)

Initializes a newly allocated set and adds to it objects from another given set.

[`-  initWithSet:copyItems:`](/documentation/Foundation/NSSet/init(set:copyItems:))

Initializes a newly allocated set and adds to it members of another given set.

[`-  init`](/documentation/Foundation/NSSet/init())

Initializes a newly allocated set.

### Counting Entries

[`count`](/documentation/Foundation/NSSet/count)

The number of members in the set.

### Accessing Set Members

[`allObjects`](/documentation/Foundation/NSSet/allObjects)

An array containing the set’s members, or an empty array if the set has no members.

[`-  anyObject`](/documentation/Foundation/NSSet/anyObject())

Returns one of the objects in the set, or `nil` if the set contains no objects.

[`-  containsObject:`](/documentation/Foundation/NSSet/contains(_:))

Returns a Boolean value that indicates whether a given object is present in the set.

[`-  filteredSetUsingPredicate:`](/documentation/Foundation/NSSet/filtered(using:))

Evaluates a given predicate against each object in the receiving set and returns a new set containing the objects for which the predicate returns true.

[`-  makeObjectsPerformSelector:`](/documentation/Foundation/NSSet/makeObjectsPerformSelector:)

Sends a message specified by a given selector to each object in the set.

[`-  makeObjectsPerformSelector:withObject:`](/documentation/Foundation/NSSet/makeObjectsPerformSelector:withObject:)

Sends a message specified by a given selector to each object in the set.

[`-  member:`](/documentation/Foundation/NSSet/member(_:))

Determines whether a given object is present in the set, and returns that object if it is.

[`-  objectEnumerator`](/documentation/Foundation/NSSet/objectEnumerator())

Returns an enumerator object that lets you access each object in the set.

[`-  enumerateObjectsUsingBlock:`](/documentation/Foundation/NSSet/enumerateObjects(_:))

Executes a given block using each object in the set.

[`-  enumerateObjectsWithOptions:usingBlock:`](/documentation/Foundation/NSSet/enumerateObjects(options:using:))

Executes a given block using each object in the set, using the specified enumeration options.

[`-  objectsPassingTest:`](/documentation/Foundation/NSSet/objects(passingTest:))

Returns a set of objects that pass a test in a given block.

[`-  objectsWithOptions:passingTest:`](/documentation/Foundation/NSSet/objects(options:passingTest:))

Returns a set of objects that pass a test in a given block, using the specified enumeration options.

### Comparing Sets

[`-  isSubsetOfSet:`](/documentation/Foundation/NSSet/isSubset(of:))

Returns a Boolean value that indicates whether every object in the receiving set is also present in another given set.

[`-  intersectsSet:`](/documentation/Foundation/NSSet/intersects(_:))

Returns a Boolean value that indicates whether at least one object in the receiving set is also present in another given set.

[`-  isEqualToSet:`](/documentation/Foundation/NSSet/isEqual(to:))

Compares the receiving set to another set.

[`-  valueForKey:`](/documentation/Foundation/NSSet/value(forKey:))

Return a set containing the results of invoking `valueForKey:` on each of the receiving set’s members.

[`-  setValue:forKey:`](/documentation/Foundation/NSSet/setValue(_:forKey:))

Invokes `setValue:forKey:` on each of the set’s members.

### Creating a Sorted Array

[`-  sortedArrayUsingDescriptors:`](/documentation/Foundation/NSSet/sortedArray(using:))

Returns an array of the set’s content sorted as specified by a given array of sort descriptors.

### Key-Value Observing

[`-  addObserver:forKeyPath:options:context:`](/documentation/Foundation/NSSet/addObserver(_:forKeyPath:options:context:))

Raises an exception.

[`-  removeObserver:forKeyPath:context:`](/documentation/Foundation/NSSet/removeObserver(_:forKeyPath:context:))

Raises an exception.

[`-  removeObserver:forKeyPath:`](/documentation/Foundation/NSSet/removeObserver(_:forKeyPath:))

Raises an exception.

### Describing a Set

[`description`](/documentation/Foundation/NSSet/description)

A string that represents the contents of the set, formatted as a property list.

[`-  descriptionWithLocale:`](/documentation/Foundation/NSSet/description(withLocale:))

Returns a string that represents the contents of the set, formatted as a property list.

## Relationships

### Conforms To

[`NSCopying`](/documentation/Foundation/NSCopying)

[`Hashable`](/documentation/Swift/Hashable)

[`Sequence`](/documentation/Swift/Sequence)

[`CustomReflectable`](/documentation/Swift/CustomReflectable)

[`NSMutableCopying`](/documentation/Foundation/NSMutableCopying)

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

[`NSFastEnumeration`](/documentation/Foundation/NSFastEnumeration)

[`Equatable`](/documentation/Swift/Equatable)

[`CustomDebugStringConvertible`](/documentation/Swift/CustomDebugStringConvertible)

[`Copyable`](/documentation/Swift/Copyable)

[`CVarArg`](/documentation/Swift/CVarArg)

[`Escapable`](/documentation/Swift/Escapable)

[`NSSecureCoding`](/documentation/Foundation/NSSecureCoding)

[`NSCoding`](/documentation/Foundation/NSCoding)

[`NSObjectProtocol`](/documentation/ObjectiveC/NSObjectProtocol)

[`ExpressibleByArrayLiteral`](/documentation/Swift/ExpressibleByArrayLiteral)

### Inherited By

[`NSMutableSet`](/documentation/Foundation/NSMutableSet)

### Inherits From

[`NSObject-swift.class`](/documentation/ObjectiveC/NSObject-swift.class)

---

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)