Mac Developer Library

Developer

Foundation Framework Reference NSMutableSet Class Reference

Options
Deployment Target:

On This Page
Language:

NSMutableSet

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.

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

    Declaration

    Objective-C

    + (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 OS X v10.0 and later.

  • Returns an initialized mutable set with a given initial capacity.

    Declaration

    Swift

    init(capacity numItems: Int)

    Objective-C

    - (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 OS X v10.0 and later.

  • init() - init Designated Initializer

    Initializes a newly allocated set.

    Declaration

    Swift

    init()

    Objective-C

    - (instancetype)init

    Return Value

    A set.

    Discussion

    This method is a designated initializer of NSMutableSet.

    Availability

    Available in OS X v10.9 and later.

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

    Declaration

    Swift

    func addObject(_ object: AnyObject)

    Objective-C

    - (void)addObject:(ObjectType)object

    Parameters

    object

    The object to add to the set.

    Availability

    Available in OS X v10.0 and later.

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

    Declaration

    Swift

    func filterUsingPredicate(_ predicate: NSPredicate)

    Objective-C

    - (void)filterUsingPredicate:(NSPredicate *)predicate

    Parameters

    predicate

    A predicate.

    Discussion

    The following example illustrates the use of this method.

    1. NSMutableSet *mutableSet =
    2. [NSMutableSet setWithObjects:@"One", @"Two", @"Three", @"Four", nil];
    3. NSPredicate *predicate =
    4. [NSPredicate predicateWithFormat:@"SELF beginswith 'T'"];
    5. [mutableSet filterUsingPredicate:predicate];
    6. // mutableSet contains (Two, Three)

    Availability

    Available in OS X v10.5 and later.

  • Removes a given object from the set.

    Declaration

    Swift

    func removeObject(_ object: AnyObject)

    Objective-C

    - (void)removeObject:(ObjectType)object

    Parameters

    object

    The object to remove from the set.

    Availability

    Available in OS X v10.0 and later.

  • Empties the set of all of its members.

    Declaration

    Swift

    func removeAllObjects()

    Objective-C

    - (void)removeAllObjects

    Availability

    Available in OS X v10.0 and later.

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

    Declaration

    Swift

    func addObjectsFromArray(_ array: [AnyObject])

    Objective-C

    - (void)addObjectsFromArray:(NSArray<ObjectType> *)array

    Parameters

    array

    An array of objects to add to the set.

    Availability

    Available in OS X v10.0 and later.