【问题标题】:iphone accessing or modifying properties of objects in an arrayiphone访问或修改数组中对象的属性
【发布时间】:2011-03-02 10:17:48
【问题描述】:

如何访问存储在数组中的对象的属性?

类似:

[myArray objectAtIndex:0].intProperty = 12345;

【问题讨论】:

    标签: iphone objective-c arrays properties


    【解决方案1】:

    您需要先投射对象。

    ((MyObjectType *) [myArray objectAtIndex:0]).intProperty = 12345;

    【讨论】:

      【解决方案2】:

      首先,您需要将 ID 存储在变量中,例如

      (id) myObject = [myArray objectAtIndex:0];
      

      然后你就可以操作它了:

      myObject.intProperty = 12345;
      

      然后再次存储:

      [myArray removeObjectAtIndex:0]; // Remove it before setting it again
      [myArray insertObject:myObject atIndex:0];
      

      编辑:或者你可以使用 Jacob 的方式,这样会更好:)

      【讨论】:

        【解决方案3】:

        通过其合成的 setter 设置属性更短(而且,在我看来,更易于阅读和理解):

        [[myArray objectAtIndex:0] setIntProperty:12345];
        

        【讨论】:

          猜你喜欢
          • 2013-05-17
          • 1970-01-01
          • 2021-09-27
          • 2015-08-03
          • 1970-01-01
          • 1970-01-01
          • 2018-05-21
          • 2018-11-12
          相关资源
          最近更新 更多