Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing An Array By Reference In Objective-C

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,

like image 211
Garry Pettet Avatar asked Nov 29 '22 05:11

Garry Pettet


2 Answers

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.

like image 81
Georg Fritzsche Avatar answered Dec 18 '22 04:12

Georg Fritzsche


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).

like image 26
dreamlax Avatar answered Dec 18 '22 05:12

dreamlax