- Inherits from:
- NSMutableSet : NSSet : NSObject
- Conforms to:
- NSCoding
- (NSSet)
- NSCopying (NSSet)
- NSMutableCopying (NSSet)
- NSObject (NSObject)
Declared in:
- Foundation/NSSet.h
An NSCountedSet object stores a modifiable set of objects, where a given object can be included in the set multiple times.
- addObject: | Adds an object to the set. |
- removeObject: | Removes an object from the set. |
The NSCountedSet class declares the programmatic interface to an object that manages a mutable set of objects. NSCountedSet provides support for the mathematical concept of a counted set. A counted set, both in its mathematical sense and in the implementation of NSCountedSet, is an unordered collection of elements, just as in a regular set, but the elements of the set aren't necessarily distinct. A counted set is also known as a bag.
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 even if the object has been added to the set multiple times. The NSSet and NSMutableSet classes are provided for static and dynamic sets (respectively) whose elements are distinct.
You add or remove objects from a counted set using the addObject: and removeObject: methods. An NSCountedSet may be queried using the objectEnumerator method, which provides for traversing elements of the set one by one. The countForObject:method returns the number of times the specified object has been added to this set.
- Initializing an NSCountedSet
- - initWithArray:
- - initWithSet:
- - initWithCapacity:
- Adding and removing entries
- - addObject:
- - removeObject:
- Accessing the members
- - countForObject:
- - objectEnumerator
- (void)addObject:(id)anObject
- (unsigned)countForObject:(id)anObject
- (id)initWithArray:(NSArray
*)anArray
See Also: - initWithArray: (NSSet), + setWithArray: (NSSet)
- (id)initWithCapacity:(unsigned)numItems
See Also: - initWithCapacity: (NSMutableSet), + setWithCapacity: (NSMutableSet)
- (id)initWithSet:(NSSet
*)aSet
See Also: - initWithSet: (NSSet), + setWithSet: (NSSet)
- (NSEnumerator *)objectEnumerator
When this method is used with a counted set, your code shouldn't modify the set during enumeration. If you intend to modify the set, use the allObjects method to create a "snapshot," then enumerate the snapshot and modify the original set.
See Also: - nextObject (NSEnumerator)
- (void)removeObject:(id)anObject
See Also: - countForObject: