NSSet Mac OS X 10.0

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 sets. The method isSubsetOfSet: will return YES if the specified set contains every member of the receiver. The method intersectsSet: returns YES if at least one member of the receiver is present in the specified set. Finally, isEqualToSet: will return YES if the contents of the receiver are equal to the contents of the specified set.

NSSet is toll-free bridged with the Core Foundation type CFSet. As such, NSSet objects can be used interchangeably with the CFSet pointer type, CFSetRef.


@interface NSSet : NSObject <NSCoding, NSCopying, NSMutableCopying>
 // Convenience Constructors
   + (id)set;
   + (id)setWithArray:(NSArray *)array;
   + (id)setWithObject:(id)object;
   + (id)setWithObjects:(id *)objs count:(unsigned)cnt;
   + (id)setWithObjects:(id)firstObj, ...;
   + (id)setWithSet:(NSSet *)set;
 // Initializers
   - (id)initWithArray:(NSArray *)array;
   - (id)initWithObjects:(id *)objects count:(unsigned)count;
   - (id)initWithObjects:(id)firstObj, ...;
   - (id)initWithSet:(NSSet *)set;
   - (id)initWithSet:(NSSet *)set copyItems:(BOOL)flag;
 // Instance Methods
   - (NSArray *)allObjects;
   - (id)anyObject;
   - (BOOL)containsObject:(id)anObject;
   - (unsigned)count;
   - (NSString *)description;
   - (NSString *)descriptionWithLocale:(NSDictionary *)locale;
   - (BOOL)intersectsSet:(NSSet *)otherSet;
   - (BOOL)isEqualToSet:(NSSet *)otherSet;
   - (BOOL)isSubsetOfSet:(NSSet *)otherSet;
   - (void)makeObjectsPerformSelector:(SEL)aSelector;
   - (void)makeObjectsPerformSelector:(SEL)aSelector withObject:(id)argument;
   - (id)member:(id)object;
   - (NSEnumerator *)objectEnumerator;
 // Methods Implementing NSCoding
   - (void)encodeWithCoder:(NSCoder *)aCoder;
   - (id)initWithCoder:(NSCoder *)aDecoder;
 // Methods Implementing NSCopying
   - (id)copyWithZone:(NSZone *)zone;
 // Methods Implementing NSMutableCopying
   - (id)mutableCopyWithZone:(NSZone *)zone;



    Part II: API Quick Reference
    Chapter 13. Foundation Classes