views:

19

answers:

3

Ok so I -roughly- want this code:

test1.m:

Foo *foo = [[Foo alloc] init];
foo.x = 1.0f;

[staticClass bar:*foo.x];

staticClass.m:

-(void)bar:(float *)argVar
{
  *argVar += 1.0f;
}

So I'm pointing the argVar to a property of the Foo class. Obivously the current code doesn't work.

What's the proper syntax for/way to do this?

+1  A: 

x is a property of Foo, not a variable. A property is just a short-hand for a pair of get/set methods. It has no address, as such, and so cannot be passed around as you are trying to do.

The simplest work-around is to go through a local variable:

float d = foo.x;
[staticClass bar:&d];
foo.x = d;

Also note that you use &, not *, to take the address of a variable.

Marcelo Cantos
They are floats, not doubles :)
Richard J. Ross III
Oops! fixed. :-)
Marcelo Cantos
Works like a charm! Thanks :)
Art
A: 

I think that the proper way to do it is this:

float tmp = foo.x;
[staticClass bar:&temp];
foo.x = tmp;

and StaticClass.m should look like this:

+(void) bar:(float *) argvar // < not plus instead of minus, denotes static method
{
     *argVar = 1.0f;
}
Richard J. Ross III
A: 

Options:

  • change Foo such that it has a float * property
  • change +[staticClass bar:] such that it takes and returns a float, updating your client code accordingly
  • use ivar_getOffset to find the location of the instance variable backing x in your Foo instance
Graham Lee