Brodie
Brodie

Reputation: 3506

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 objectAtIndex:0].intProperty = 12345;

Upvotes: 1

Views: 1107

Answers (3)

ele
ele

Reputation: 6181

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

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

Upvotes: 0

Emil
Emil

Reputation: 7256

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 :)

Upvotes: 0

Jacob Relkin
Jacob Relkin

Reputation: 163258

You need to cast the object first.

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

Upvotes: 6

Related Questions