<!--
{
  "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/NSMutableSet",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSMutableSet"
  },
  "title" : "NSMutableSet"
}
-->

# NSMutableSet

A dynamic unordered collection of unique objects.

```
class NSMutableSet
```

## Overview

You can use this type in Swift instead of a <doc://com.apple.documentation/documentation/Swift/Set> in cases that require reference semantics.

The `NSMutableSet` class declares the programmatic interface to a mutable, unordered collection of distinct objects.

The [`NSCountedSet`](/documentation/Foundation/NSCountedSet) class, which is a concrete subclass of `NSMutableSet`, supports mutable sets that can contain multiple instances of the same element. The [`NSSet`](/documentation/Foundation/NSSet) class supports creating and managing immutable sets.

NSMutableSet is “toll-free bridged” with its Core Foundation counterpart, <doc://com.apple.documentation/documentation/CoreFoundation/CFMutableSet>. 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.

### Subclassing Notes

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

#### Methods to Override

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

- [`add(_:)`](/documentation/Foundation/NSMutableSet/add(_:))
- [`remove(_:)`](/documentation/Foundation/NSMutableSet/remove(_:))

You must also override the primitive methods of the [`NSSet`](/documentation/Foundation/NSSet) class.

## Topics

### Creating a mutable set

[`+  setWithCapacity:`](/documentation/Foundation/NSMutableSet/setWithCapacity:)

Creates and returns a mutable set with a given initial capacity.

[`-  initWithCapacity:`](/documentation/Foundation/NSMutableSet/init(capacity:))

Returns an initialized mutable set with a given initial capacity.

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

Initializes a newly allocated set.

### Adding and removing entries

[`-  addObject:`](/documentation/Foundation/NSMutableSet/add(_:))

Adds a given object to the set, if it is not already a member.

[`-  filterUsingPredicate:`](/documentation/Foundation/NSMutableSet/filter(using:))

Evaluates a given predicate against the set’s content and removes from the set those objects for which the predicate returns false.

[`-  removeObject:`](/documentation/Foundation/NSMutableSet/remove(_:))

Removes a given object from the set.

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

Empties the set of all of its members.

[`-  addObjectsFromArray:`](/documentation/Foundation/NSMutableSet/addObjects(from:))

Adds to the set each object contained in a given array that is not already a member.

### Combining and recombining sets

[`-  unionSet:`](/documentation/Foundation/NSMutableSet/union(_:))

Adds each object in another given set to the receiving set, if not present.

[`-  minusSet:`](/documentation/Foundation/NSMutableSet/minus(_:))

Removes each object in another given set from the receiving set, if present.

[`-  intersectSet:`](/documentation/Foundation/NSMutableSet/intersect(_:))

Removes from the receiving set each object that isn’t a member of another given set.

[`-  setSet:`](/documentation/Foundation/NSMutableSet/setSet(_:))

Empties the receiving set, then adds each object contained in another given set.

## Relationships

### Inherits From

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

### Conforms To

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

### Inherited By

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

---

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)