Friday, June 21, 2013

NSMutableSet initWithCapacity example in Objective C (iOS).

NSMutableSet initWithCapacity

Returns an initialized mutable set with a given initial capacity.

- (id)initWithCapacity:(NSUInteger)numItems

The initial capacity of the set.

Return Value of [NSMutableSet initWithCapacity]
An initialized mutable set with initial capacity to hold numItems members. The returned set might be different than the original receiver.

Discussion of [NSMutableSet initWithCapacity]
Mutable sets allocate additional memory as needed, so numItems simply establishes the object’s initial capacity.

NSMutableSet initWithCapacity example.
You probably want

NSMutableSet *set = [[NSMutableSet alloc] initWithCapacity: someNumber];

NSMutableSet *set = [NSMutableSet setWithCapacity: someNumber];

Example of [NSMutableSet initWithCapacity].
NSSet* tom = [[NSMutableSet alloc] initWithCapacity:1];
NSSet* dick = [[NSMutableSet alloc] initWithCapacity:1];
NSSet* harry = [tom setByAddingObjectsFromSet:dick];

printf("tom   retainCount: %d \n", [tom retainCount]);
printf("dick  retainCount: %d \n", [dick retainCount]);
printf("harry retainCount: %d \n", [harry retainCount]);

NSMutableSet initWithCapacity example.
NSMutableSet* collectibleCopy = [[NSMutableSet alloc] initWithCapacity: [_collectibles count]];
for ( id thing in _collectibles ) {
    [collectibleCopy addObject: thing];

End of NSMutableSet initWithCapacity example article.