Reputation: 5698
I have following code:
[[NSMutableArray alloc] initWithCapacity:slidingValue]
However, if I try to get the size of it by using count
directly after above initialisation, it will return 0. There is no adding object into it yet though.
Am I able to get the capacity of it?
Upvotes: 1
Views: 691
Reputation: 1960
You don't need the capacity. It's an optimization, and depending on how large a capacity you specify and what internal algorithm NSArray
chooses, might actually be ignored. Just add the objects when you get them, using -addObject:
or -insertObject:
. Different from a CFArray
, the capacity of an NSArray
isn't a hard limit. It's just a suggestion.
If you get your objects in a random order (but with an index number), you could create an array and fill it with [NSNull null]
objects using -addObject:
, then replace the NSNull
with the actual object as you get it. But the latter is rarely needed.
Note:
The -initWithCapacity:
method creates an empty array with a hint of how large the array can reach before a memory reallocation. The count increases when you actually -addObject
: to the array.
Upvotes: 1