I have this problem with Cocoa, I am calling a function and passing an Array to it:
Some where I call the function:
[self processLabels:labels];
And the function is as follow:
- (void)processLabels:(NSMutableArray*)labs{
labs = [[NSMutableArray alloc] init];
[labs addObject:@"Random"];
....
}
When debugging, I notice that no new object are being added to labels when they are added to labs. Is it because I am re-initializing labs? how could I re-initialize labels inside the function then?
I tried using byref by didn’t succeed,
any help is appreciated..
thanks
The statement
labs = [[NSMutableArray alloc] init];makeslabsto 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:
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:Or, if
processLabels:is just returning a collection of labels: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
allocornew, or contain the wordcopy.