开发者

iphone accessing or modifying properties of objects in an array

How would I access the properties of an object stored in an array?

something like:

[myArray objectAt开发者_StackOverflow社区Index:0].intProperty = 12345;


You need to cast the object first.

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


First, you will need to store the ID in a variable, like

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

Then you can manipulate it:

myObject.intProperty = 12345;

And store it again:

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

EDIT: Or you could use Jacob's way, which is much better :)


Setting the property by its synthesized setter is shorter (and, to my eyes, cleaner to read and comprehend):

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜