A mutable, unordered collection of distinct objects that may appear more than once in the collection.


@interface NSCountedSet<__covariant ObjectType> : NSMutableSet


Each distinct object inserted into an NSCountedSet object has a counter associated with it. NSCountedSet keeps track of the number of times objects are inserted and requires that objects be removed the same number of times. Thus, there is only one instance of an object in an NSSet object even if the object has been added to the set multiple times. The count method defined by the superclass NSSet has special significance; it returns the number of distinct objects, not the total number of times objects are represented in the set. The NSSet and NSMutableSet classes are provided for static and dynamic sets, respectively, whose elements are distinct.

While NSCountedSet and CFBagRef are not toll-free bridged, they provide similar functionality. For more information about CFBagRef, see the CFBag.

Subclassing Notes

Because NSCountedSet is not a class cluster, it does not have primitive methods that provide the basis for its implementation. In general, there should be little need for subclassing.

Methods to Override

If you subclass NSCountedSet, you must override any method of which you want to change the behavior.

If you change the primitive behavior of an NSCountedSet, for instance if you change how objects are stored, you must override all of the affected methods. These include:

If you change the primitive behavior, you must also override the primitive methods of NSSet and NSMutableSet.


Initializing a Counted Set

- initWithArray:

Returns a counted set object initialized with the contents of a given array.

- initWithSet:

Returns a counted set object initialized with the contents of a given set.

- initWithCapacity:

Returns a counted set object initialized with enough memory to hold a given number of objects.

Adding and Removing Entries

- addObject:

Adds a given object to the set.

- removeObject:

Removes a given object from the set.

Combining and Recombining Sets

- unionSet:

Adds each object in another given set to the receiving set, if not present.

- minusSet:

Removes each object in another given set from the receiving set, if present.

- intersectSet:

Removes from the receiving set each object that isn’t a member of another given set.

Examining a Counted Set

- countForObject:

Returns the count associated with a given object in the set.

- objectEnumerator

Returns an enumerator object that lets you access each object in the set once, independent of its count.


Inherits From

See Also

Specialized Sets


A static, ordered collection of unique objects.


A dynamic, ordered collection of unique objects.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software