Abstract against release

When I need an array for temporary use, what's the difference between them:

1

NSMutableArray *stuff = [[NSMutableArray alloc] init]; // use the array [stuff release]; 

2:

 NSMutableArray *stuff = [NSMutableArray array]; // use the array 

3:

 NSMutableArray *stuff = [[[NSMutableArray alloc] init] autorelease]; // use the array 

I prefer number 2 as it is shorter. Are there any good reasons to use number 1 or 3?

+5
source share
1 answer

In most cases, number 2 is probably the best choice.

Number 1 has the ability to lose a release at some point for some reason, but for some reason it immediately frees an array that can be useful in memory environments.

Number 3 is basically the detailed equivalent of number 2, but it is useful if you want to use initWith *, which does not have a corresponding array with *.

Note. If you are hungry in memory, for example, in an expensive loop, where you need a fresh array for each iteration; Do not allocate or allocate new arrays; just use -removeAllObjects and recycle the array.

+10
source

Source: https://habr.com/ru/post/888515/


All Articles