Name

NSSet — Mac OS X 10.0

Synopsis

This class implements a unordered collection of unique objects. NSSet is based on the mathematical idea of a set, where each member is unique, and the order of elements is unimportant. What is important in a set is object membership. NSSet objects are often used in situations where an application needs to quickly determine whether or not an object is a member of a collection. As such, NSSet is able to more efficiently make this determination than NSArray.

To test whether or not an object is a member of set, use the method containsObject:, which returns a BOOL. Alternatively, the method member can be used, which returns the specified object if it exists in the set, and nil otherwise.

To enumerate the contents of a set, we create an instance of NSEnumerator by sending an objectEnumerator message to the set. Note that the order that objects are accessed by the enumerator is not guaranteed, and an order should not be assumed.

Often we want to invoke some method in each member of a collection. NSSet provides a method that saves us from the burden of having to enumerate the contents of the set and send the message to each object manually. This method is makeObjectsPerformSelector:, which will cause the method matching the selector to be invoked in each member of the collection. If you need to invoke a method that takes an argument, then use the method makeObjectsPerformSelector:withObject:.

NSSet provides several methods that are useful for comparing two ...

Get Cocoa in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.