Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

I am developing an iphone app and I was wondering the following:

How do I initialise an NSMutableArray of NSMutableArrays to a certain size?

Sorry for the noob question

share|improve this question
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:5];

for (int i = 0; i < 5; i++) {
   NSMutableArray *innerArray = [[NSMutableArray alloc] initWithCapacity:5];

   [array addObject:innerArray];
}
share|improve this answer
4  
Note that initWithCapacity: is just setting an initial capacity. The array can continue to grow as much as you like (it's an optimization tool) – Nektarios Jul 17 '11 at 2:51
1  
Of course, it is implied because it is a mutable array. – Wayne Hartman Jul 17 '11 at 3:07
1  
For someone that's new I don't think that's obvious. C arrays are certainly mutable and certainly don't have built-in expansion of available space – Nektarios Jul 17 '11 at 3:10

You don't set a maximum size. You just keep addObject:'ing to them

share|improve this answer
    
Exactly, NSMutableArray don't need initWithCapacity. Never. – Farini Feb 13 '12 at 21:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.