代码之家  ›  专栏  ›  技术社区  ›  Art

向Objective-C中的接口属性发送指针

  •  0
  • Art  · 技术社区  · 14 年前

    测试1.m:

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

    静态类m:

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

    所以我将argVar指向Foo类的一个属性。很遗憾,当前代码不起作用。

    正确的语法/方法是什么?

    3 回复  |  直到 14 年前
        1
  •  1
  •   Richard J. Ross III    14 年前

    正确的做法是:

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

    +(void) bar:(float *) argvar // < not plus instead of minus, denotes static method
    {
         *argVar = 1.0f;
    }
    
        2
  •  1
  •   Marcelo Cantos    14 年前

    x Foo ,不是变量。属性只是一对get/set方法的简写。它没有地址,所以不能像你想的那样到处传递。

    最简单的解决方法是遍历局部变量:

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

    还请注意,您使用 & ,不是 * ,获取变量的地址。

        3
  •  0
  •   user23743 user23743    14 年前

    • 改变 Foo float * 财产
    • 改变 +[staticClass bar:]
    • 使用 ivar_getOffset 查找实例变量备份的位置 x 在你的