3

This is my init method:

-(id)init{

    self = [super init];
    magicNumber = 8;

    myMagicArray = [[NSMutableArray alloc] initWithCapacity:(magicNumber*magicNumber)];
    NSLog(@"this is the magic Array: %d", [myMagicArray count]);

    return self;
}

This is the .h:

@interface Magic : NSObject {
    NSMutableArray *myMagicArray;
    int magicNumber;

}

The console shows me that number is 0. instead of 64, wt's happen? I already check out this post:

StackOverflow Link: https://stackoverflow.com/questions/633699/nsmutablearray-count-always-returns-zero

4 Answers 4

8

You're confusing capacity with count. The capacity is only the memory space reserved for the array, so when the array expands it doesn't need to take time to allocate memory.

The count is the actual number of items stored in the array.

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.


,———.———.———.———————————————————————————————————.
| 4 | 6 | 8 | <—— room for array to expand ———> |
'———'———'———'                                   |
| count = 3                                     |
|                                               |
'——— memory reserved (capacity) of the array ———'
                        > 3
Sign up to request clarification or add additional context in comments.

2 Comments

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.
it would be nice to be able to iterate object insertion like this: for(int i=0; i<array.capacity; i++) - is there no way?
1

The "initWithCapacity" method reserves excess capacity so that subsequent insertions don't have to resize the array (until you've overshot the initially reserved capacity), while "count" tells you the actual number of elements in the array (i.e. the number that you've inserted) and not the total capacity available.

Comments

0

When you init the myMagicArray, you're creating memory space for it... in this case enough memory to hold 64 objects. But you haven't actually added any objects to it yet so the count is 0 until you add an object.

1 Comment

So, can I get the capacity number?
0

You can't access the capacity property, and it doesn't represent populated size (as eloquently described by kennytm).

One approach might be to derive a class from NSMutableArray and intercept the initWithCapacity method, to record the initialCapacity and then churn it out in a property. But if you're in a rush, you can use this simple method below:

Create a function:

NSMutableArray* createArrayWithSize(NSUInteger number)
{
    NSMutableArray* array = [[NSMutableArray alloc] initWithCapacity:number];
    for( NSUInteger i=0;i<number; i++ )
    {
        array[i]=[NSNull null];
    }
    return array;
}

Then initialise your NSMutableArray as follows.

myMagicArray = createArrayWithSize(10);

[myMagicArray count] will now be 10.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.