NSMutableSet Class Reference

Inherits from
Conforms to
Framework
/System/Library/Frameworks/Foundation.framework
Availability
Available in iOS 2.0 and later.
Companion guide
Declared in
NSPredicate.h
NSSet.h
Related sample code

Overview

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, CFMutableSetRef. 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.

Tasks

Creating a Mutable Set

Adding and Removing Entries

Combining and Recombining Sets

Class Methods

setWithCapacity:

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

+ (instancetype)setWithCapacity:(NSUInteger)numItems
Parameters
numItems

The initial capacity of the new set.

Return Value

A mutable set with initial capacity to hold numItems members.

Discussion

Mutable sets allocate additional memory as needed, so numItems simply establishes the object’s initial capacity.

Availability
  • Available in iOS 2.0 and later.
Declared In
NSSet.h

Instance Methods

addObject:

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

- (void)addObject:(id)object
Parameters
object

The object to add to the set.

Availability
  • Available in iOS 2.0 and later.
Related Sample Code
Declared In
NSSet.h

addObjectsFromArray:

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

- (void)addObjectsFromArray:(NSArray *)array
Parameters
array

An array of objects to add to the set.

Availability
  • Available in iOS 2.0 and later.
Declared In
NSSet.h

filterUsingPredicate:

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

- (void)filterUsingPredicate:(NSPredicate *)predicate
Parameters
predicate

A predicate.

Discussion

The following example illustrates the use of this method.

NSMutableSet *mutableSet =
    [NSMutableSet setWithObjects:@"One", @"Two", @"Three", @"Four", nil];
NSPredicate *predicate =
    [NSPredicate predicateWithFormat:@"SELF beginswith 'T'"];
[mutableSet filterUsingPredicate:predicate];
// mutableSet contains (Two, Three)
Availability
  • Available in iOS 3.0 and later.
Declared In
NSPredicate.h

init

Initializes a newly allocated set.

- (instancetype)init
Return Value

A set.

Discussion

This method is a designated initializer of NSMutableSet.

Availability
  • Available in iOS 7.0 and later.
Declared In
NSSet.h

initWithCapacity:

Returns an initialized mutable set with a given initial capacity.

- (instancetype)initWithCapacity:(NSUInteger)numItems
Parameters
numItems

The initial capacity of the set.

Return Value

An initialized mutable set with initial capacity to hold numItems members. The returned set might be different than the original receiver.

Discussion

Mutable sets allocate additional memory as needed, so numItems simply establishes the object’s initial capacity.

This method is a designated initializer for NSMutableSet.

Availability
  • Available in iOS 2.0 and later.
Declared In
NSSet.h

intersectSet:

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

- (void)intersectSet:(NSSet *)otherSet
Parameters
otherSet

The set with which to perform the intersection.

Availability
  • Available in iOS 2.0 and later.
Declared In
NSSet.h

minusSet:

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

- (void)minusSet:(NSSet *)otherSet
Parameters
otherSet

The set of objects to remove from the receiving set.

Availability
  • Available in iOS 2.0 and later.
Declared In
NSSet.h

removeAllObjects

Empties the set of all of its members.

- (void)removeAllObjects
Availability
  • Available in iOS 2.0 and later.
Declared In
NSSet.h

removeObject:

Removes a given object from the set.

- (void)removeObject:(id)object
Parameters
object

The object to remove from the set.

Availability
  • Available in iOS 2.0 and later.
Related Sample Code
Declared In
NSSet.h

setSet:

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

- (void)setSet:(NSSet *)otherSet
Parameters
otherSet

The set whose members replace the receiving set's content.

Availability
  • Available in iOS 2.0 and later.
Declared In
NSSet.h

unionSet:

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

- (void)unionSet:(NSSet *)otherSet
Parameters
otherSet

The set of objects to add to the receiving set.

Availability
  • Available in iOS 2.0 and later.
Declared In
NSSet.h