# NSCountedSet class reference

mccallum@gnu.ai.mit.edu
Generated at 2025-12-02 21:52:00 +0000
1995, 1996, 1998 Free Software Foundation, Inc.
Software documentation for the NSCountedSet class
Foundation/NSSet.h

The `NSCountedSet` class is used to maintain a set of objects where the number of times each object has been added (without a corresponding removal) is kept track of.

In GNUstep, the `purge` and `unique` methods are provided to make use of a counted set for uniquing objects easier.

countForObject:
anObject
Returns the number of times that an object that is equal to the specified object (as determined by the [NSObject -isEqual:] method) has been added to the set and not removed from it.
Software documentation for the NSMutableSet class
Foundation/NSSet.h
Mutable version of NSSet .
setWithCapacity:
numItems
New autoreleased instance with given capacity.
addObject:
anObject
Adds anObject to the set.
The object is retained by the set.
addObjectsFromArray:
array
Adds all the objects in the array to the receiver.
initWithCapacity:
numItems
Initialises a newly allocated set to contain no objects but to have space available to hold the specified number of items.
Additions of items to a set initialised with an appropriate capacity will be more efficient than addition of items otherwise.
Calls -init (which does nothing but maintain MacOS-X compatibility), and needs to be re-implemented in subclasses in order to have all other initialisers work.
intersectSet:
other
Removes from the receiver all the objects it contains which are not also in other.
minusSet:
other
Removes from the receiver all the objects that are in other.
removeAllObjects
Removes all objects from the receiver.
removeObject:
anObject
Removes the anObject from the receiver.
setSet:
other
Removes all objects from the receiver then adds the objects from other. If the receiver is other, the method has no effect.
unionSet:
other
* Adds all the objects from other to the receiver.
Software documentation for the NSSet class
Foundation/NSSet.h
NSCoding
NSCopying
NSMutableCopying
NSFastEnumeration
NSSet
maintains an unordered collection of unique objects (according to [NSObject -isEqual:] ). When a duplicate object is added to the set, it replaces its old copy.
set
New autoreleased empty set.
setWithArray:
objects
New set containing (unique elements of) objects.
setWithObject:
anObject
New set containing single object anObject.
setWithObjects:
firstObject
New set with objects in given nil-terminated list.
setWithObjects:
objects
count:
count
New set containing (unique elements of) objects.
setWithSet:
aSet
Copy constructor.
allObjects
Return array of all objects in set. Order is undefined.
anyObject
Return an arbitrary object from set, or `nil` if this is empty set.
containsObject:
anObject
Return whether set contains an object equal to this one according to [NSObject -isEqual:] .
count
Returns the number of objects stored in the set.
description
Returns listing of objects in set.
descriptionWithLocale:
locale
Returns listing of objects in set.
enumerateObjectsUsingBlock:
aBlock
Enumerate over the collection using a given block. The first argument is the object. The second argument is a pointer to a BOOL indicating whether the enumeration should stop. Setting this to `YES` will interupt the enumeration.
enumerateObjectsWithOptions:
opts
usingBlock:
aBlock
Enumerate over the collection using the given block. The first argument is the object. The second argument is a pointer to a BOOL indicating whether the enumeration should stop. Setting this to `YES` will interrupt the enumeration. The opts argument is a bitfield. Setting the NSNSEnumerationConcurrent flag specifies that it is thread-safe. The NSEnumerationReverse bit specifies that it should be enumerated in reverse order.
init

In MacOS-X class clusters do not have designated initialisers, and there is a general rule that -init is treated as the designated initialiser of the class cluster, but that other intitialisers may not work s expected an would need to be individually overridden in any subclass.

GNUstep tries to make it easier to subclass a class cluster, by making class clusters follow the same convention as normal classes, so the designated initialiser is the richest initialiser. This means that all other initialisers call the documented designated initialiser (which calls -init only for MacOS-X compatibility), and anyone writing a subclass only needs to override that one initialiser in order to have all the other ones work.

For MacOS-X compatibility, you may also need to override various other initialisers. Exactly which ones, you will need to determine by trial on a MacOS-X system... and may vary between releases of MacOS-X. So to be safe, on MacOS-X you probably need to re-implement all the class cluster initialisers you might use in conjunction with your subclass.

initWithArray:
other
Initialises a newly allocated set by adding all the objects in the supplied array to the set.
initWithObjects:
firstObject
Initialize with (unique elements of) objects in given nil-terminated list.
initWithObjects:
objects
count:
count
Initialize to contain (unique elements of) objects.
Calls -init (which does nothing but maintain MacOS-X compatibility), and needs to be re-implemented in subclasses in order to have all other initialisers work.
initWithSet:
other
Initialize with same items as other (items not copied).
initWithSet:
other
copyItems:
flag
Initialises a newly allocated set by adding all the objects in the supplied set.
intersectsSet:
otherSet
Return whether set intersection with otherSet is non-empty.
isEqualToSet:
other
Return whether each set is subset of the other.
isSubsetOfSet:
otherSet
Return whether subset of otherSet.
makeObjectsPerform:
aSelector
Send each object given message (with no arguments). Identical to -makeObjectsPerformSelector: .
makeObjectsPerform:
aSelector
withObject:
argument
Send each object given message with given argument. Identical to -makeObjectsPerformSelector:withObject:.
makeObjectsPerformSelector:
aSelector
Send each object given message (with no arguments). Identical to -makeObjectsPerform: .
makeObjectsPerformSelector:
aSelector
withObject:
argument
Send each object given message with given argument. Identical to -makeObjectsPerform:withObject: .
member:
anObject
If anObject is in set, return it (the copy in the set).
objectEnumerator
Return enumerator over objects in set. Order is undefined.
objectsPassingTest:
aBlock
Description forthcoming.
objectsWithOptions:
opts
passingTest:
aBlock
Description forthcoming.
setByAddingObject:
anObject
Return a set formed by adding anObject to the receiver.
setByAddingObjectsFromArray:
other
Return a set formed by adding the contents of other to the receiver.
setByAddingObjectsFromSet:
other
Return a set formed as a union of the receiver and other.
Software documentation for the NSCountedSet(GNU_Uniquing) category
Foundation/NSSet.h
Utility methods for using a counted set to handle uniquing of objects.
purge:
level

This method removes from the set all objects whose count is less than or equal to the specified value.

This is useful where a counted set is used for uniquing objects. The set can be periodically purged of objects that have only been added once - and are therefore simply wasting space.

unique:
anObject

If the supplied object (or one equal to it as determined by the [NSObject -isEqual:] method) is already present in the set, the count for that object is incremented, the supplied object is released, and the object in the set is retained and returned. Otherwise, the supplied object is added to the set and returned.

This method is useful for uniquing objects - the init method of a class need simply end with - `return [myUniquingSet unique: self];`