views:

5392

answers:

5

I have a for-loop. Inside that loop I want to fill up an NSArray with some objects. But I dont see any method that would let me do that. I know in advance how many objects there are. I want to avoid an NSMutableArray, since some people told me that's a very big overhead and performance-brake compared to NSArray.

I've got something like this:

NSArray *returnArray = [[NSArray alloc] init];
for (imageName in imageArray) {
    UIImage *image = [UIImage imageNamed:imageName];
    //now, here I'd like to add that image to the array...
}

I looked in the doc for NSArray but couldn't see something that lets me specify how many elements are going to be in there... any idea? Or must I really use NSMutableArray for that?

+3  A: 

Would this be a good time to use an NSMutableArray?

I know you mentioned that you would like to avoid it, but sometimes there's a reason for things like this.

Wally Lawless
+2  A: 

You'll need to use an NSMutableArray for that because adding is mutation and NSArray is immutable.

You could make a populated NSMutableArray into an NSArray afterwards (see here for a discussion) but you won't be adding items to a regular old NSArray anytime soon.

Kris
+6  A: 

Yes, you need to use an NSMutableArray:

int count = [imageArray count];
NSMutableArray *returnArray = [[NSMutableArray alloc] initWithCapacity:count];
for (imageName in imageArray) {
    UIImage *image = [UIImage imageNamed:imageName];
    [returnArray addObject: image];
    ...
}

EDIT - declaration fixed

Alnitak
Thanks. Are you sure that the data structure of the variable is of type NSArray, and not NSMutableArray? Or are those interchangeable?
Thanks
NSMutableArray is a subclass of NSArray, so it's safe.
Alnitak
Yeah, I forgot. Thanks! That raises another question ;)
Thanks
Since it's being used as a mutable array, returnArray should be declared as an NSMutableArray. This code will work since the object really is the mutable variety, but it's bad practice (and will give a warning about NSArray not responding to addObject:).
smorgan
So if that's bad practice, of course I'll do NSMutableArray *returnArray = [[NSMutableArray alloc] initWithCapacity:count]; ;)
Thanks
+1  A: 

I would advise optimising only when you know NSMutableArray is causing you a performance hit but if it is, you can always create a static aray from a mutable array after you have populated it. It depends on if the performance hit is caused by you subsequent use of the NSMutableArray or just by creating it.

My guess is that this isn't going to be the performance bottleneck on your app.

Roger Nolan
A: 

I have the same problem, thanks for the info. I have one more question. How do you allocate and deallocate memory for an NSMutableArray and where do you place this code? Another thing, how do you access the images in this array through index? Thank you very much in advance.

Memory is automatically allocated for you when you do `[[NSMutableArray] alloc] init]`, and when you do `addObject:` it will also allocate the extra memory needed.Finally, to access an element at a certain index you can do `[myArray objectAtIndex:index]`.
Itay