The statement labs = [[NSMutableArray alloc] init];
makes labs
to point to the new array in the scope of the method. It does not make the caller's pointer point to the new array.
If you want to change the caller's pointer, do something like this:
// The caller
NSMutableArray *labels; // Don't initialize *labels.
[self processLabels:&labels];
...
- (void)processLabels:(NSMutableArray**)labs{
*labs = [[NSMutableArray alloc] init];
[*labs addObject:@"Random"];
...
}
That's probably a bad idea because processLabels:
allocates the array but the caller is responsible for freeing it.
If you want the caller to own the array, you could write processLabels:
like this:
- (void)processLabels:(NSMutableArray*)labs{
[labs removeAllObjects];
[labs addObject:@"Random"];
...
}
Or, if processLabels:
is just returning a collection of labels:
- (NSMutableArray*)processLabels {
NSMutableArray* labs = [[[NSMutableArray alloc] init] autorelease];
[labs addObject:@"Random"];
...
return labs;
}
If you want the caller to be responsible for freeing the array, remove the autorelease. In that case, convention dictates that the method name should start with alloc
or new
, or contain the word copy
.