4

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

2 Answers 2

11
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:5];

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

   [array addObject:innerArray];
}
Sign up to request clarification or add additional context in comments.

2 Comments

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)
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
3

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

1 Comment

Exactly, NSMutableArray don't need initWithCapacity. Never.

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.