0

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]];
Matt Cooper
  • 2,042
  • 27
  • 43
  • Many Cocoa classes have mutable and immutable forms -- NSString, NSArray, NSDictionary, et al. When you look at the documentation for, say, [NSString](http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html) you will see, in the second paragraph, mention of the mutable form, and a "hot link" to its description. That's where all "goodies" are hidden. (But, while looking at the mutable description, you also need to reference back to the non-mutable form to find, eg, `rangeOfString` or some other query type function.) – Hot Licks Nov 24 '12 at 01:36

4 Answers4

3

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:

NSMutableArray *doors = [NSMutableArray new];
for (int i=0; i < 100; i++){
    [doors addObject:[[Door alloc]init]];
}

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:

NSArray *newDoorList = [NSArray arrayWithArray:doors];

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.

CocoaEv
  • 2,984
  • 20
  • 21
  • Thanks! Yeah I'm not worried about performance for this specific implementation, but I was curious and figured I should find out how to do it for bigger future projects. – Matt Cooper Nov 24 '12 at 01:31
  • your welcome. I use mutable arrays frequently just because their syntax makes them easier to read and write. I will usually convert them to a plain NSArray if I'm sending them down the line in a segue or something of the like and the array is becoming a shared datasource, otherwise its not really a big deal. Hardcore dev's might argue that point and its something to keep in mind if you are developing on a team - it might be a pet peeve of one of the devs. best of luck. – CocoaEv Nov 24 '12 at 01:39
1

Create an NSMutableArray and then use [array copy] to get a non-mutable NSArray.

Alejandro
  • 3,726
  • 1
  • 23
  • 29
1

See this answer for some options, basically create an NSMutable array and use that to create your NSArray...

http://www.cocoabuilder.com/archive/cocoa/190315-nsmutablearray-to-nsarray.html#190321

IrishDubGuy
  • 1,053
  • 2
  • 9
  • 18
1

NSMutableArray is a dynamic array, and you don't need to predefine a size. As Aleph suggests, use an NSMutableArray and append your door object 100x. Like this:

 NSMutableArray *myArray = [NSMutableArray arrayWithCapacity:100];

 for ( int i = 0; i < 100; i++ ) {

      [myArray addObject: [[Door alloc] init]];
 }

 NSArray *filledArray = [NSArray arrayWithArray:myArray];

Just edited to include the arrayWithCapacity: method. Per this SO thread, if you know the exact number you need, it should just allocate the memory all at once. Besides that, NSArray vs. NSMutableArray isn't going to show that much difference speed-wise. ( Until proven wrong. O_o )

Disadvantage of using NSMutableArray vs NSArray?

Community
  • 1
  • 1
Miles Alden
  • 1,542
  • 1
  • 11
  • 21