An object representing a dynamic, unordered, uniquing collection, for use instead of a Set variable in cases that require reference semantics.


class NSMutableSet : NSSet


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

The NSCountedSet class, which is a concrete subclass of NSMutableSet, supports mutable sets that can contain multiple instances of the same element. The NSSet class supports creating and managing immutable sets.

NSMutableSet is “toll-free bridged” with its Core Foundation counterpart, CFMutableSet. See Toll-Free Bridging 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:

You must also override the primitive methods of the NSSet class.


Creating a mutable set

init(capacity: Int)

Returns an initialized mutable set with a given initial capacity.


Initializes a newly allocated set.

Adding and removing entries

func add(Any)

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

func filter(using: NSPredicate)

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

func remove(Any)

Removes a given object from the set.

func removeAllObjects()

Empties the set of all of its members.

func addObjects(from: [Any])

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

Combining and recombining sets

func union(Set<AnyHashable>)

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

func minus(Set<AnyHashable>)

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

func intersect(Set<AnyHashable>)

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

func setSet(Set<AnyHashable>)

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

See Also


class NSSet

An object representing a static, unordered, uniquing collection, for use instead of a Set constant in cases that require reference semantics.