Looked around for a while and couldn’t find an answer to this, but it seems like a pretty simple thing. I want to create an NSArray that has room for 100 objects (Doors) and then just loop through to create a new door for each index in the array. I couldn’t find a way to do this without manually writing the code for 100 new doors when I init the NSArray. I know I could do this by just creating an NSMutableArray and using addObject but I’ve heard NSArrays are much faster and I’d like to know how to do this for future reference.
Here’s what I’m basically trying to do:
NSArray *doors = [[NSArray alloc]init]; //with size 100?
for (int i = 0; i < [doors count]; i++)
[[doors objectAtIndex:i] = [[Door alloc]init]];
if you are going to add objects to an array inside of a loop, then NSMutableArray is the correct object to use.
To create 100 doors:
Now you have an mutable array with 100 Door Objects.
If you have no need to later modify the array, you can convert it back to
an immutable array for processing like this:
There may be some minor performance hits between mutable and non mutable arrays, but you will not notice them with just a few hundred objects – at least that has been my experience.
hope that helps. good luck.