Mas
Mas

Reputation:

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

I have a very basic question. I'm a new iPhone programmer. My question is can anybody tell me how can I pass values by reference to a function in obj. C? I know how to do it in VB and C#. But don't know how to do it in Obj c.

Thanks

Upvotes: 44

Views: 63792

Answers (6)

Niranjala S
Niranjala S

Reputation: 89

Try this way :

    -(void)secondFunc:(NSInteger*)i
    {
      *j=20;
     //From here u can change i value as well
     //now i and j value is same (i.e i=j=20)
    }
    -(void) firstFunc
    {
       NSInteger i=5;
      [self secondFunc :&i];
     //i value is 20
    }

Hope it helps!!

Upvotes: 7

stephen
stephen

Reputation: 1061

There's an argument to be had if you want to get more than one address from a resulting function call. You can do so instead by creating a wrapper class and passing that instead. For example:

@interface AddressWrapper : NSObject {
    NSArray     *array;
    NSString    *string;
}

@property (nonatomic, assign)   NSArray *array;
@property (nonatomic, assign)   NSString *string;
@end

In *.m:

@implementation AddressWrapper
@synthesize array, string;
@end



-(void) getAddresses: (AddressWrapper*) wrapper {
    wrapper.array = myNSArray;
    wrapper.string = myNSString;
}

Upvotes: 0

Peter N Lewis
Peter N Lewis

Reputation: 17811

If you use Objective C++, which you can do by naming your file with extension .mm, or telling Xcode to compile all source as Objective C++, then you can pass references in the same way you do with C++, eg:

- (OSStatus) fileFileInfo: (FileInfo&)fi;

Upvotes: 16

Darren
Darren

Reputation: 25619

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

Upvotes: 91

Chuck
Chuck

Reputation: 237110

It depends on what you mean. The closest thing C (and thus Objective-C) has to a reference type is pointers. As the name implies, pointers point to data that exists somewhere else. In order to get the thing being pointed to, you have to dereference the pointer. Objects in Objective-C are never directly accessed — you always use pointers to talk to them. You can also pass pointers to other types than objects.

Pointers are kind of a tricky subject. I would definitely recommend reading up on C to get a feel for them.

Upvotes: 0

ttvd
ttvd

Reputation: 1675

There is no passing by reference (in C++ sense) in Objective C. You can pass your objects by pointer, and for primitive types by value.

Example:

void func(NSString* string)
{
  ...
}

void func(NSInteger myint)
{
 ...
}

..
NSString* str = @"whatever";
NSInteger num = 5;

func(str);
func(num); 

Upvotes: 14

Related Questions