The correct way to return an array

I don’t seem to understand that. I have a method that returns a volatile array. What is the correct way to return an array and avoid possible memory leaks?

If I plan to store the results locally inside another view controller, does this affect the way the array is returned?

Finally, what if it's just not a mutable array? Does this require a different technique?

thanks howie

+3
source share
3 answers

, - . , , .

- (NSMutabalArray*] mutableArray {
    return [[myArray mutableCopy] autorelease];
}

- (NSArray*] array {
    return [[myArray copy] autorelease];
}
+4

. alloc/init/copy, autorelease , (- return [myArray autorelease];). , factory (arrayFrom... arrayWithContentsOf...), , .

apple dev. , , .

+2

NSMutableArray :

-(NSMutableArray*)getMyArray
{
   NSMutableArray *retval = [[NSMutableArray alloc] init];
   // do your stuff w/ array
   return [retval autorelease];
}

The caller of this code may want the retainreturned array because it is auto-implemented.

+1
source

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


All Articles