views:

179

answers:

2

I would like to pass a NSMutableArray by reference so that it can be altered by another method. What would be the correct syntax for this?

Thanks,

+11  A: 

Objective-C objects are always passed by reference (using pointers) - you can't pass them by value.

I.e. the following is fine:

- (void)mutateArray:(NSMutableArray*)array {
    // alter array ...
}

... and can be e.g. invoked like this:

NSMutableArray *array = ...;
[self mutateArray:array];

There is also the possibility of passing a pointer by reference:

- (void)newArray:(NSMutableArray **)array;

In that case array is used as an out-parameter - you pass a reference to a pointer to receive an instance:

- (void)newArray:(NSMutableArray **)array {
    *array = [[NSMutableArray alloc] init];
}

... which could be called like so:

NSMutableArray *array = nil;
[self newArray:&array];

Using out-parameters is usually only seen if the return-value is already used and additional information has to be returned. An example would be error-information as dreamlax noted.

Georg Fritzsche
What does it mean (using your above code as an example) when the following appears in a method declaration: `- (void)mutateArray:(NSMutableArray **)array;`?
Garry
@Garry: Added - does that clear it up?
Georg Fritzsche
Perfectly. Many thanks.
Garry
+4  A: 

In addition to Georg Fritzche's answer, it may be worth noting that some methods expect to be given the address of an object pointer. For example:

NSError *anError; // points to garbage now
NSStringEncoding enc;
NSString *aString = [NSString stringWithContentsOfFile:@"/some/file.txt"
                                          usedEncoding:&enc
                                                 error:&anError];

if (aString == nil)
{
    // anError now points to an initialised NSError object.
}

It gets tricky because some documented methods require you to release objects obtained in this manner, and some don't (for an example of one that does require explicit releasing, see NSPropertyListSerialization).

dreamlax
Any method that requires you to release the returned pointer in this way *should* have explicit documentation, because it violates the fundamental rule of Cocoa reference counted memory management.
JeremyP