Class

NSSet

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

Overview

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

Symbols

Creating a Set

init(object: Any)

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

func adding(Any)

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

func addingObjects(from: Set<AnyHashable>)

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

func addingObjects(from: [Any])

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

Initializing a Set

init(array: [Any])

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

init(objects: UnsafePointer<AnyObject>!, count: Int)

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

init(set: Set<AnyHashable>)

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

init(set: Set<AnyHashable>, copyItems: Bool)

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

init()

Initializes a newly allocated set.

Counting Entries

var count: Int

The number of members in the set.

Accessing Set Members

var allObjects: [Any]

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

func anyObject()

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

func contains(Any)

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

func filtered(using: NSPredicate)

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.

func member(Any)

Determines whether the set contains an object equal to a given object, and returns that object if it is present.

func objectEnumerator()

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

func enumerateObjects((Any, UnsafeMutablePointer<ObjCBool>) -> Void)

Executes a given block using each object in the set.

func enumerateObjects(options: NSEnumerationOptions = [], using: (Any, UnsafeMutablePointer<ObjCBool>) -> Void)

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

func objects(passingTest: (Any, UnsafeMutablePointer<ObjCBool>) -> Bool)

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

func objects(options: NSEnumerationOptions = [], passingTest: (Any, UnsafeMutablePointer<ObjCBool>) -> Bool)

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

Comparing Sets

func isSubset(of: Set<AnyHashable>)

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

func intersects(Set<AnyHashable>)

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

func isEqual(to: Set<AnyHashable>)

Compares the receiving set to another set.

func value(forKey: String)

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

func setValue(Any?, forKey: String)

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

Creating a Sorted Array

func sortedArray(using: [NSSortDescriptor])

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

Describing a Set

var description: String

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

func description(withLocale: Any?)

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

Instance Properties