iOS Developer Library

Developer

Foundation Framework Reference NSMutableSet Class Reference

Options
Deployment Target:

On This Page
Language:

NSMutableSet

Inheritance


Import Statement


Swift

import Foundation

Objective-C

@import Foundation;

Availability


Available in iOS 2.0 and later.

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.

    Import Statement

    Objective-C

    @import Foundation;

    Availability

    Available in iOS 2.0 and later.

  • init(capacity:) - initWithCapacity: Designated Initializer

    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.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.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.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 7.0 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:(id)object

    Parameters

    object

    The object to add to the set.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.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.

    • NSMutableSet *mutableSet =
    • [NSMutableSet setWithObjects:@"One", @"Two", @"Three", @"Four", nil];
    • NSPredicate *predicate =
    • [NSPredicate predicateWithFormat:@"SELF beginswith 'T'"];
    • [mutableSet filterUsingPredicate:predicate];
    • // mutableSet contains (Two, Three)

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 3.0 and later.

  • Removes a given object from the set.

    Declaration

    Swift

    func removeObject(_ object: AnyObject)

    Objective-C

    - (void)removeObject:(id)object

    Parameters

    object

    The object to remove from the set.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later.

  • Empties the set of all of its members.

    Declaration

    Swift

    func removeAllObjects()

    Objective-C

    - (void)removeAllObjects

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.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 *)array

    Parameters

    array

    An array of objects to add to the set.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later.

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

    Declaration

    Swift

    func unionSet(_ otherSet: Set<NSObject>)

    Objective-C

    - (void)unionSet:(NSSet *)otherSet

    Parameters

    otherSet

    The set of objects to add to the receiving set.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later.

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

    Declaration

    Swift

    func minusSet(_ otherSet: Set<NSObject>)

    Objective-C

    - (void)minusSet:(NSSet *)otherSet

    Parameters

    otherSet

    The set of objects to remove from the receiving set.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later.

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

    Declaration

    Swift

    func intersectSet(_ otherSet: Set<NSObject>)

    Objective-C

    - (void)intersectSet:(NSSet *)otherSet

    Parameters

    otherSet

    The set with which to perform the intersection.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later.

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

    Declaration

    Swift

    func setSet(_ otherSet: Set<NSObject>)

    Objective-C

    - (void)setSet:(NSSet *)otherSet

    Parameters

    otherSet

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

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later.