Inherits from | |
Conforms to | |
Framework | /System/Library/Frameworks/Foundation.framework |
Availability | Available in Mac OS X v10.0 and later. |
Companion guide | |
Declared in | NSKeyValueCoding.h NSKeyValueObserving.h NSPredicate.h NSSet.h |
Related sample code |
The NSSet
, NSMutableSet
, and NSCountedSet
classes declare the programmatic interface to an object that manages a set of objects. NSSet
provides support for the mathematical concept of a set. A set, both in its mathematical sense and in the implementation of NSSet
, is an unordered collection of distinct elements. The NSMutableSet
(a subclass of NSSet
) and NSCountedSet
(a subclass of NSMutableSet
) classes are provided for sets whose contents may be altered.
NSSet
and NSMutableSet
are part of a class cluster, so sets are not actual instances of NSSet
or NSMutableSet
. Rather, the instances belong to one of their private subclasses. (For convenience, we use the term set to refer to any one of these instances without specifying its exact class membership.) Although a set’s class is private, its interface is public, as declared by the abstract superclasses NSSet
and NSMutableSet
. Note that NSCountedSet
is not part of the class cluster; it is a concrete subclass of NSMutableSet
.
NSSet
declares the programmatic interface for static sets of objects. You establish a static set’s entries when it’s created, and thereafter the entries can’t be modified. NSMutableSet
, on the other hand, declares a programmatic interface for dynamic sets of 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.
Objects in a set must respond to the NSObject
protocol methods hash
and isEqual:
—see the NSObject
protocol for more information.
Note that if mutable objects are stored in a set, either the hash
method of the objects shouldn’t depend on the internal state of the mutable objects or the mutable objects shouldn’t be modified while they’re in the set (note that it can be difficult to know whether or not a given object is in a collection).
Objects added to a set are not copied; rather, an object receives a retain
message before it’s added to a set.
Typically, you create a temporary set by sending one of the set…
methods to the NSSet
class object. These methods return an NSSet
object containing the elements (if any) you pass in as arguments. The set
method is a “convenience” method to create an empty mutable set.
The set classes adopt the NSCopying
and NSMutableCopying
protocols, making it convenient to convert a set of one type to the other.
NSSet
provides methods for querying the elements of the set. allObjects
returns an array containing the objects in a set. anyObject
returns some object in the set. count
returns the number of objects currently in the set. member:
returns the object in the set that is equal to a specified object. Additionally, intersectsSet:
tests for set intersection, isEqualToSet:
tests for set equality, and isSubsetOfSet:
tests for one set being a subset of another.
The objectEnumerator
method provides for traversing elements of the set one by one. For better performance on Mac OS X v10.5 and later, you can also use the Objective-C fast enumeration feature (see Fast Enumeration).
NSSet
’s makeObjectsPerformSelector:
and makeObjectsPerformSelector:withObject:
methods provides for sending messages to individual objects in the set.
NSSet
is “toll-free bridged” with its Core Foundation counterpart, CFSet Reference. This means that the Core Foundation type is interchangeable in function or method calls with the bridged Foundation object. Therefore, in a method where you see an NSSet *
parameter, you can pass a CFSetRef
, and in a function where you see a CFSetRef
parameter, you can pass an NSSet
instance (you cast one type to the other to suppress compiler warnings). See Interchangeable Data Types for more information on toll-free bridging.
+ set
+ setWithArray:
+ setWithObject:
+ setWithObjects:
+ setWithObjects:count:
+ setWithSet:
– setByAddingObject:
– setByAddingObjectsFromSet:
– setByAddingObjectsFromArray:
– initWithArray:
– initWithObjects:
– initWithObjects:count:
– initWithSet:
– initWithSet:copyItems:
– allObjects
– anyObject
– containsObject:
– filteredSetUsingPredicate:
– makeObjectsPerformSelector:
– makeObjectsPerformSelector:withObject:
– member:
– objectEnumerator
Creates and returns an empty set.
+ (id)set
A new empty set.
This method is declared primarily for the use of mutable subclasses of NSSet
.
+ setWithArray:
+ setWithObject:
+ setWithObjects:
– setByAddingObject:
– setByAddingObjectsFromSet:
– setByAddingObjectsFromArray:
NSSet.h
Creates and returns a set containing a uniqued collection of those objects contained in a given array.
+ (id)setWithArray:(NSArray *)anArray
An array containing the objects to add to the new set. If the same object appears more than once in anArray, it is added only once to the returned set. Each object receives a retain
message as it is added to the set.
A new set containing a uniqued collection of those objects contained in anArray.
+ set
+ setWithObject:
+ setWithObjects:
– setByAddingObject:
– setByAddingObjectsFromSet:
– setByAddingObjectsFromArray:
NSSet.h
Creates and returns a set that contains a single given object.
+ (id)setWithObject:(id)anObject
The object to add to the new set. anObject receives a retain
message after being added to the set.
A new set that contains a single member, anObject.
+ set
+ setWithArray:
+ setWithObjects:
– setByAddingObject:
– setByAddingObjectsFromSet:
– setByAddingObjectsFromArray:
NSSet.h
Creates and returns a set containing the objects in a given argument list.
+ (id)setWithObjects:(id)anObject, ...
The first object to add to the new set.
A comma-separated list of objects, ending with nil
, to add to the new set. If the same object appears more than once in the list of objects, it is added only once to the returned set. Each object receives a retain
message as it is added to the set.
A new set containing the objects in the argument list.
As an example, the following code excerpt creates a set containing three different types of elements (assuming aPath
exits):
NSSet *mySet; |
NSData *someData = [NSData dataWithContentsOfFile:aPath]; |
NSValue *aValue = [NSNumber numberWithInteger:5]; |
NSString *aString = @"a string"; |
mySet = [NSSet setWithObjects:someData, aValue, aString, nil]; |
+ set
+ setWithArray:
+ setWithObject:
– setByAddingObject:
– setByAddingObjectsFromSet:
– setByAddingObjectsFromArray:
NSSet.h
Creates and returns a set containing a specified number of objects from a given C array of objects.
+ (id)setWithObjects:(id *)objects count:(NSUInteger)count
A C array of objects to add to the new set. If the same object appears more than once in objects, it is added only once to the returned set. Each object receives a retain
message as it is added to the set.
The number of objects from objects to add to the new set.
A new set containing count objects from the list of objects specified by objects.
+ set
+ setWithArray:
+ setWithObject:
+ setWithObjects:
– setByAddingObject:
– setByAddingObjectsFromSet:
– setByAddingObjectsFromArray:
NSSet.h
Creates and returns a set containing the objects from another set.
+ (id)setWithSet:(NSSet *)aSet
A set containing the objects to add to the new set. Each object receives a retain
message as it is added to the new set.
A new set containing the objects from aSet.
+ set
+ setWithArray:
+ setWithObject:
+ setWithObjects:
– setByAddingObject:
– setByAddingObjectsFromSet:
– setByAddingObjectsFromArray:
NSSet.h
Raises an exception.
- (void)addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context
The object to register for KVO notifications. The observer must implement the key-value observing method observeValueForKeyPath:ofObject:change:context:
.
The key path, relative to the receiver, of the property to observe. This value must not be nil
.
A combination of the NSKeyValueObservingOptions
values that specifies what is included in observation notifications. For possible values, see NSKeyValueObservingOptions
.
Arbitrary data that is passed to observer in observeValueForKeyPath:ofObject:change:context:
.
NSSet
objects are not observable, so this method raises an exception when invoked on an NSSet
object. Instead of observing a set, observe the unordered to-many relationship for which the set is the collection of related objects.
NSKeyValueObserving.h
Returns an array containing the receiver’s members, or an empty array if the receiver has no members.
- (NSArray *)allObjects
An array containing the receiver’s members, or an empty array if the receiver has no members. The order of the objects in the array isn’t defined.
NSSet.h
Returns one of the objects in the receiver, or nil
if the receiver contains no objects.
- (id)anyObject
One of the objects in the receiver, or nil
if the receiver contains no objects. The object returned is chosen at the receiver’s convenience—the selection is not guaranteed to be random.
NSSet.h
Returns a Boolean value that indicates whether a given object is present in the receiver.
- (BOOL)containsObject:(id)anObject
The object for which to test membership of the receiver.
YES
if anObject is present in the receiver, otherwise NO
.
NSSet.h
Returns the number of members in the receiver.
- (NSUInteger)count
The number of members in the receiver.
NSSet.h
Returns a string that represents the contents of the receiver, formatted as a property list.
- (NSString *)description
A string that represents the contents of the receiver, formatted as a property list.
NSSet.h
Returns a string that represents the contents of the receiver, formatted as a property list.
- (NSString *)descriptionWithLocale:(id)locale
In Mac OS X v10.4 and earlier, this must be a dictionary that specifies options used for formatting each of the receiver’s members. In Mac OS X v10.5 and later, you can use an NSLocale
object. If you do not want the receiver’s members to be formatted, specify nil
.
A string that represents the contents of the receiver, formatted as a property list.
This method sends each of the receiver’s members descriptionWithLocale:
with locale passed as the sole parameter. If the receiver’s members do not respond to descriptionWithLocale:
, this method sends description
instead.
NSSet.h
Evaluates a given predicate against each object in the receiver and returns a new set containing the objects for which the predicate returns true.
- (NSSet *)filteredSetUsingPredicate:(NSPredicate *)predicate
A predicate.
A new set containing the objects in the receiver for which predicate returns true.
The following example illustrates the use of this method.
NSSet *sourceSet = |
[NSSet setWithObjects:@"One", @"Two", @"Three", @"Four", nil]; |
NSPredicate *predicate = |
[NSPredicate predicateWithFormat:@"SELF beginswith 'T'"]; |
NSSet *filteredSet = |
[sourceSet filteredSetUsingPredicate:predicate]; |
// filteredSet contains (Two, Three) |
NSPredicate.h
Initializes a newly allocated set with the objects that are contained in a given array.
- (id)initWithArray:(NSArray *)array
An array of objects to add to the new set. If the same object appears more than once in array, it is represented only once in the returned set. Each object receives a retain
message as it is added to the set.
An initialized object, which might be different than the original receiver.
NSSet.h
Initializes a newly allocated set with members taken from the specified list of objects.
- (id)initWithObjects:(id)firstObj, ...
The first object to add to the new set.
A comma-separated list of objects, ending with nil
, to add to the new set. If the same object appears more than once in the list, it is represented only once in the returned set. Each object receives a retain
message as it is added to the set
An initialized object, which might be different than the original receiver.
NSSet.h
Initializes a newly allocated set with a specified number of objects from a given C array of objects.
- (id)initWithObjects:(id *)objects count:(NSUInteger)count
A C array of objects to add to the new set. If the same object appears more than once in objects, it is added only once to the returned set. Each object receives a retain
message as it is added to the set.
The number of objects from objects to add to the new set.
An initialized object, which might be different than the original receiver.
This method is the designated initializer for NSSet
.
NSSet.h
Initializes a newly allocated set and adds to it objects from another given set.
- (id)initWithSet:(NSSet *)otherSet
A set containing objects to add to the receiver. Each object is retained as it is added to the receiver.
An initialized object, which might be different than the original receiver.
NSSet.h
Initializes a newly allocated set and adds to it members of another given set.
- (id)initWithSet:(NSSet *)otherSet copyItems:(BOOL)flag
A set containing objects to add to the new set.
If YES
, the members of otherSet are copied, and the copies are added to the receiver. If NO
, the members of otherSet are added to the receiver and retained.
An initialized object that contains the members of otherSet.
This method returns an initialized object, which might be different than the original receiver.
Note that, if flag is YES
, copyWithZone:
is invoked to make copies—thus, the receiver’s new member objects may be immutable, even though their counterparts in otherSet were mutable. Also, members must conform to the NSCopying
protocol)
NSSet.h
Returns a Boolean value that indicates whether at least one object in the receiver is also present in another given set.
- (BOOL)intersectsSet:(NSSet *)otherSet
The set with which to compare the receiver.
YES
if at least one object in the receiver is also present in otherSet, otherwise NO
.
NSSet.h
Compares the receiver to another set.
- (BOOL)isEqualToSet:(NSSet *)otherSet
The set with which to compare the receiver.
YES
if the contents of otherSet are equal to the contents of the receiver, otherwise NO
.
Two sets have equal contents if they each have the same number of members and if each member of one set is present in the other.
– intersectsSet:
– isEqual:
(NSObject
protocol)– isSubsetOfSet:
NSSet.h
Returns a Boolean value that indicates whether every object in the receiver is also present in another given set.
- (BOOL)isSubsetOfSet:(NSSet *)otherSet
The set with which to compare the receiver.
YES
if every object in the receiver is also present in otherSet, otherwise NO
.
NSSet.h
Sends to each object in the receiver a message specified by a given selector.
- (void)makeObjectsPerformSelector:(SEL)aSelector
A selector that specifies the message to send to the members of the receiver. The method must not take any arguments. It should not have the side effect of modifying the receiver. This value must not be NULL
.
The message specified by aSelector is sent once to each member of the receiver. This method raises an NSInvalidArgumentException
if aSelector is NULL
.
NSSet.h
Sends to each object in the receiver a message specified by a given selector.
- (void)makeObjectsPerformSelector:(SEL)aSelector withObject:(id)anObject
A selector that specifies the message to send to the receiver's members. The method must take a single argument of type id
. The method should not, as a side effect, modify the receiver. The value must not be NULL
.
The object to pass as an argument to the method specified by aSelector.
The message specified by aSelector is sent, with anObject as the argument, once to each member of the receiver. This method raises an NSInvalidArgumentException
if aSelector is NULL
.
NSSet.h
Determines whether the receiver contains an object equal to a given object, and returns that object if it is present.
- (id)member:(id)anObject
The object for which to test for membership of the receiver.
If the receiver contains an object equal to anObject (as determined by isEqual:
) then that object (typically this will be anObject), otherwise nil
.
If you override isEqual:
, you must also override the hash
method for the member:
method to work on a set of objects of your class.
NSSet.h
Returns an enumerator object that lets you access each object in the receiver.
- (NSEnumerator *)objectEnumerator
An enumerator object that lets you access each object in the receiver.
The following code fragment illustrates how you can use this method.
NSEnumerator *enumerator = [mySet objectEnumerator]; |
id value; |
while ((value = [enumerator nextObject])) { |
/* code that acts on the set’s values */ |
} |
When this method is used with mutable subclasses of NSSet
, your code shouldn’t modify the receiver during enumeration. If you intend to modify the receiver, use the allObjects
method to create a “snapshot” of the set’s members. Enumerate the snapshot, but make your modifications to the original set.
– nextObject
(NSEnumerator
)NSSet.h
Raises an exception.
- (void)removeObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath
The object to remove as an observer.
A key-path, relative to the receiver, for which observer is registered to receive KVO change notifications. This value must not be nil
.
NSSet
objects are not observable, so this method raises an exception when invoked on an NSSet
object. Instead of observing a set, observe the unordered to-many relationship for which the set is the collection of related objects.
NSKeyValueObserving.h
Returns a new set formed by adding a given object to the collection defined by the receiver.
- (NSSet *)setByAddingObject:(id)anObject
The object to add to the collection defined by the receiver.
A new set formed by adding anObject to the collection defined by the receiver.
+ set
+ setWithArray:
+ setWithObject:
+ setWithObjects:
– setByAddingObjectsFromSet:
– setByAddingObjectsFromArray:
NSSet.h
Returns a new set formed by adding the objects in a given array to the collection defined by the receiver.
- (NSSet *)setByAddingObjectsFromArray:(NSArray *)other
The array of objects to add to the collection defined by the receiver.
A new set formed by adding the objects in other to the collection defined by the receiver.
+ set
+ setWithArray:
+ setWithObject:
+ setWithObjects:
– setByAddingObject:
– setByAddingObjectsFromSet:
NSSet.h
Returns a new set formed by adding the objects in a given set to the collection defined by the receiver.
- (NSSet *)setByAddingObjectsFromSet:(NSSet *)other
The set of objects to add to the collection defined by the receiver.
A new set formed by adding the objects in other to the collection defined by the receiver.
+ set
+ setWithArray:
+ setWithObject:
+ setWithObjects:
– setByAddingObject:
– setByAddingObjectsFromSet:
NSSet.h
Invokes setValue:forKey:
on each of the receiver’s members.
- (void)setValue:(id)value forKey:(NSString *)key
The value for the property identified by key.
The name of one of the properties of the receiver's members.
NSKeyValueCoding.h
Return a set containing the results of invoking valueForKey:
on each of the receiver's members.
- (id)valueForKey:(NSString *)key
The name of one of the properties of the receiver's members.
A set containing the results of invoking valueForKey:
(with the argument key) on each of the receiver's members.
The returned set might not have the same number of members as the receiver. The returned set will not contain any elements corresponding to instances of valueForKey:
returning nil
(note that this is in contrast with NSArray
’s implementation, which may put NSNull
values in the arrays it returns).
NSKeyValueCoding.h
© 2008 Apple Inc. All Rights Reserved. (Last updated: 2008-10-15)