Return to the Alphabetic Index
Return to the Class Browser
Return to the Picture Browser
Copyright (c) 1994 by NeXT Computer, Inc. All Rights Reserved.


Inherits From: NSObject

Conforms To: NSCoding, NSCopying, NSMutableCopying NSObject (NSObject)

Declared In: Foundation/NSSet.h

Class Description

The NSSet class declares the programmatic interface to an object that manages an immutable set of objects. NSSet provides support for the mathematical concept of a set. A set, both in its mathematical sense and in the OpenStep implementation of NSSet, is an unordered collection of distinct elements. OpenStep provides the NSMutableSet class for sets whose contents may be altered, and also provides the NSCountedSet class for sets that can contain multiple instances of the same element.

Use set objects as an alternative to array objects when the order of elements is not important, but performance in testing whether an object is contained in the set is a considerationwhile arrays are ordered, testing for membership is slower than with sets. For example, the NSSet method containsObject: operates in O(1) time when applied to a set, while containsObject: operates in O(N) time when applied to an array.

Objects in a set must respond to hash and isEqual: methods. See the NSObject protocol for details on hash and isEqual:.

Generally, you instantiate an NSSet object by sending one of the set1/4 methods to the NSSet class object. These methods return an NSSet object containing the elements (if any) you pass in as arguments. The set method is a convenience method to create an empty set. Newly created instances of NSSet created by invoking the set method can be populated with objects using any of the init1/4 methods. initWithObjects:: is the designated initializer for the NSSet class. Objects added to the set are not copied; rather, each object receives a retain message before it's added to the set.

NSSet provides methods for querying the elements of the set. allObjects returns an array containing all objects in the set. anyObject returns some object in the set. count returns the number of objects currently in the set. member: returns the object in the set that is equal to a specified object. Additionally, the intersectsSet: tests for set intersection, isEqualToSet: tests for set equality, and isSubsetOfSet: tests for one set being a subset of the specified set object.

The objectEnumerator method provides for traversing elements of the set one by one.

NSSet's makeObjectsPerform: and makeObjectsPerform:withObject: methods provides for sending messages to individual objects in the set.


NSSet implements the encodeWithCoder: method, which raises NSInternalInconsistencyException if the number of objects enumerated for encoding turns out to be unequal to the number of objects in the set.

Allocating and Initializing a Set

Querying the Set

Sending Messages to Elements of the Set

Comparing Sets

Creating a String Description of the Set