O'Reilly logo

Cocoa in a Nutshell by James Duncan Davidson, Michael Beam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required