A static unordered collection of unique objects.
- iOS 2.0+
- macOS 10.0+
- UIKit for Mac 13.0+Beta
- tvOS 9.0+
- watchOS 2.0+
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.
NSMutable, 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.
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
Alternatives to Subclassing
Before making a custom class of
NSHash and the corresponding Core Foundation type, CFSet. Because
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,
NSSet do not have identical interfaces or implementations, and you can sometimes do things with
CFSet that you cannot easily do with
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.