【发布时间】:2011-03-02 10:17:48
【问题描述】:
如何访问存储在数组中的对象的属性?
类似:
[myArray objectAtIndex:0].intProperty = 12345;
【问题讨论】:
标签: iphone objective-c arrays properties
如何访问存储在数组中的对象的属性?
类似:
[myArray objectAtIndex:0].intProperty = 12345;
【问题讨论】:
标签: iphone objective-c arrays properties
您需要先投射对象。
((MyObjectType *) [myArray objectAtIndex:0]).intProperty = 12345;
【讨论】:
首先,您需要将 ID 存储在变量中,例如
(id) myObject = [myArray objectAtIndex:0];
然后你就可以操作它了:
myObject.intProperty = 12345;
然后再次存储:
[myArray removeObjectAtIndex:0]; // Remove it before setting it again
[myArray insertObject:myObject atIndex:0];
编辑:或者你可以使用 Jacob 的方式,这样会更好:)
【讨论】:
通过其合成的 setter 设置属性更短(而且,在我看来,更易于阅读和理解):
[[myArray objectAtIndex:0] setIntProperty:12345];
【讨论】: