When adding objects to an NSMutableArray
in a loop, what are some best practices as to reuse memory allocated for objects that are to be added to the array?
For example:
for (int i = 0; i < 5; i++)
{
SampleObject *sampleObject = [[SampleObject alloc] init];
sampleObject.someProperty = ...
[sampleObjectArray addObject:sampleObject];
[sampleObject release];
}
Is this the correct approach? I have the sampleObjectArray
as a property with (nonatomic, retain) on it. Should it be copy instead of retain?
Wouldn't it be better to alloc out of the loop and then release when the loop completes?