How to pass values by reference in objective C (iphone)

Pass-by-reference in Objective-C is the same as it is in C.

The equivalent to the following C# code:

void nullThisObject(ref MyClass foo)
{
    foo = null;
}

MyClass bar = new MyClass();
this.nullThisObject(ref bar);
assert(bar == null);

is

- (void)nilThisObject:(MyClass**)foo
{
    [*foo release];
    *foo = nil;
}

MyClass* bar = [[MyClass alloc] init];
[self nilThisObject:&bar];
NSAssert(bar == nil);

and

- (void)zeroThisNumber:(int*)num
{
    *num = 0;
}

int myNum;
[self zeroThisNumber:&myNum];
NSAssert(myNum == 0);

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)