Initializing NSMutableArray with and without capacity

Possible duplicate:
NSMutableArray nuances initWithCapacity
Objective-c NSArray init and initWithCapacity: 0

What is the difference between the following line of code? What is the exact advantage and disadvantage? Assume the next one I will do operation 3 addObject.

NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity: 5];
NSMutableArray *array = [[NSMutableArray alloc] init];
+5
source share
2 answers

Functionally, both operators are identical.

In the first case, you give the tooltip a hint that you will soon add five objects to the array so that it can, if it wants, preallocate some space for them. This means that the first five calls addObject:can be a bit faster using the first statement.

, , . initWithCapacity:. - , addObject: , , , .

+8

- : , , .

- , , 5 ( ), . , , , , , , . , , .

Apple:

arrayWithCapacity:

NSMutableArray , .... . NSMutableArray numItems.

+5