Class

NSSet

A static unordered collection of unique objects.

Declaration

@interface NSSet<__covariant ObjectType> : NSObject

Overview

The NSSet, NSMutableSet, and NSCountedSet classes declare the programmatic interface to an unordered collection of objects.

NSSet declares the programmatic interface for static sets of distinct objects. You establish a static set’s entries when it’s created, and thereafter the entries can’t be modified. NSMutableSet, on the other hand, declares a programmatic interface for dynamic sets of distinct objects. A dynamic—or mutable—set allows the addition and deletion of entries at any time, automatically allocating memory as needed.

You can use sets as an alternative to arrays when the order of elements isn’t important and performance in testing whether an object is contained in the set is a consideration—while arrays are ordered, testing for membership is slower than with sets.

NSSet is “toll-free bridged” with its Core Foundation counterpart, CFSetRef. See Toll-Free Bridging for more information on toll-free bridging.

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 all of its primitive methods:

Alternatives to Subclassing

Before making a custom class of NSSet, investigate NSHashTable and the corresponding Core Foundation type, CFSet. Because NSSet and CFSet are “toll-free bridged,” you can substitute a CFSet object for a NSSet object in your code (with appropriate casting). Although they are corresponding types, CFSet and NSSet do not have identical interfaces or implementations, and you can sometimes do things with CFSet that you cannot easily do with NSSet.

If the behavior you want to add supplements that of the existing class, you could write a category on NSSet. Keep in mind, however, that this category will be in effect for all instances of NSSet that you use, and this might have unintended consequences. Alternatively, you could use composition to achieve the desired behavior.

Topics

Creating a Set

+ set

Creates and returns an empty set.

+ setWithArray:

Creates and returns a set containing a uniqued collection of the objects contained in a given array.

+ setWithObject:

Creates and returns a set that contains a single given object.

+ setWithObjects:

Creates and returns a set containing the objects in a given argument list.

+ setWithObjects:count:

Creates and returns a set containing a specified number of objects from a given C array of objects.

+ setWithSet:

Creates and returns a set containing the objects from another set.

- setByAddingObject:

Returns a new set formed by adding a given object to the receiving set.

- setByAddingObjectsFromSet:

Returns a new set formed by adding the objects in a given set to the receiving set.

- setByAddingObjectsFromArray:

Returns a new set formed by adding the objects in a given array to the receiving set.

Initializing a Set

- initWithArray:

Initializes a newly allocated set with the objects that are contained in a given array.

- initWithObjects:

Initializes a newly allocated set with members taken from the specified list of objects.

- initWithObjects:count:

Initializes a newly allocated set with a specified number of objects from a given C array of objects.

- initWithSet:

Initializes a newly allocated set and adds to it objects from another given set.

- initWithSet:copyItems:

Initializes a newly allocated set and adds to it members of another given set.

- init

Initializes a newly allocated set.

Counting Entries

count

The number of members in the set.

Accessing Set Members

allObjects

An array containing the set’s members, or an empty array if the set has no members.

- anyObject

Returns one of the objects in the set, or nil if the set contains no objects.

- containsObject:

Returns a Boolean value that indicates whether a given object is present in the set.

- filteredSetUsingPredicate:

Evaluates a given predicate against each object in the receiving set and returns a new set containing the objects for which the predicate returns true.

- makeObjectsPerformSelector:

Sends a message specified by a given selector to each object in the set.

- makeObjectsPerformSelector:withObject:

Sends a message specified by a given selector to each object in the set.

- member:

Determines whether a given object is present in the set, and returns that object if it is.

- objectEnumerator

Returns an enumerator object that lets you access each object in the set.

- enumerateObjectsUsingBlock:

Executes a given block using each object in the set.

- enumerateObjectsWithOptions:usingBlock:

Executes a given block using each object in the set, using the specified enumeration options.

- objectsPassingTest:

Returns a set of object that pass a test in a given block.

- objectsWithOptions:passingTest:

Returns a set of object that pass a test in a given block, using the specified enumeration options.

Comparing Sets

- isSubsetOfSet:

Returns a Boolean value that indicates whether every object in the receiving set is also present in another given set.

- intersectsSet:

Returns a Boolean value that indicates whether at least one object in the receiving set is also present in another given set.

- isEqualToSet:

Compares the receiving set to another set.

- valueForKey:

Return a set containing the results of invoking valueForKey: on each of the receiving set's members.

- setValue:forKey:

Invokes setValue:forKey: on each of the set’s members.

Creating a Sorted Array

- sortedArrayUsingDescriptors:

Returns an array of the set’s content sorted as specified by a given array of sort descriptors.

Key-Value Observing

Describing a Set

description

A string that represents the contents of the set, formatted as a property list.

- descriptionWithLocale:

Returns a string that represents the contents of the set, formatted as a property list.

See Also

Basic Collections

NSArray

A static ordered collection of objects.

NSMutableArray

A dynamic ordered collection of objects.

NSDictionary

A static collection of objects associated with unique keys.

NSMutableDictionary

A dynamic collection of objects associated with unique keys.

NSMutableSet

A dynamic unordered collection of unique objects.