0

Say have "normal" class in iOS, called A. I'd like to pass this as a parameter to class B, method "myMethod".

"A" is declared as a pointer:

A       *myA;

I'm unclear as to the syntax of call. Here's the method (I think this is probably correct:

- (void *) myMethod:(A *) myA 

Should the call be

[b myMethod:myA]

or

[b myMethod:&myA]

Since myA is a pointer, it's seems it should be the second one, but somehow I'm thinking it's the first.

1 Answer 1

1

It's the first:

[b myMethod:myA]

I rarely dereference pointers in Objective-C as I used to do in C/C++.

3
  • Thanks. But I'm still confused - the "&" extracts the address, which should be what the pointer declarations receives. Why doesn't this have to be done? Sep 1, 2011 at 19:32
  • when you write a method like -(void)method:(MyObject*)obj it receives a variable of type MyObject*. When you pass a parameter in to that method, you define it as MyObject *myObj. So it's the same going in as it is coming out. You might be getting confused over the fact that C++ has dot (.) and arrow (->) operators. Objective-C doesn't have this distinction. Sep 1, 2011 at 20:35
  • one last thing... if you were to pass in &myObj into the method, you are taking myObj (which is defined as MyObject* and taking its address, which is an address of an address. So you are effectively passing in a MyObject**. Sep 1, 2011 at 20:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.