- Joined
- Sep 22, 2011
- Messages
- 2
- Reaction score
- 0
- Points
- 1
As newbie, Im stuck on getting the values from objects I have put in a NSMutableArray. To give a full picture of my problem I would appreciate you reading the following:
So lets say I produce a new object as follows (writing to console):
Person *player = [[Person alloc] init];
player.age = 10;
player.height = 10;
player.name = @"player";
As I continue to write my programme I can change the above values of player (not at run time) i.e
player.age = 23; etc.....
If I want to create another Person (player2) I repeat the above like this:
Person *player1 = [[Person alloc] init];
player1.age = 13;
player1.height = 4;
player1.name = @"player1";
In my programme I can now change and compare values of the 2 objects i.e.
if (player.age == player1.age) bla bla bla
My problem starts if I want to create 20+ Person objects - I know how to place all the objects in a loop/NSMutableArray as follows:
for (int i = 0; i < 20; i++)
{ Person *player = [[Person alloc] init];
player.age = 10;
player.height = 10;
player.name = @"player";
[myArray addObjectlayer];
[player release];
}
All the objects in myArray are individual but have the same values. Thats fine ! But how to get or change the objects in myArray ? (not at run time)
If [myArray objectAtIndex:4]; holds the following values:
player.age = 10;
player.height = 10;
player.name = @"player";
how do I get to the object(s) in myArray so I can compare/sort/add etc..
Heres example of what I want to do but I know is wrong:
if (player.age == [myArray personAtIndex:15.age]; // wrong !
or
NSLog(@"@ has a height of %i",[myArray personAtIndex:15:name:height];
prints out >> person has a height of 10
I really would appreciate you helping me on this - As a newbie I'm finding it hard to move on until I get an understanding of this problem. Thanks again.
So lets say I produce a new object as follows (writing to console):
Person *player = [[Person alloc] init];
player.age = 10;
player.height = 10;
player.name = @"player";
As I continue to write my programme I can change the above values of player (not at run time) i.e
player.age = 23; etc.....
If I want to create another Person (player2) I repeat the above like this:
Person *player1 = [[Person alloc] init];
player1.age = 13;
player1.height = 4;
player1.name = @"player1";
In my programme I can now change and compare values of the 2 objects i.e.
if (player.age == player1.age) bla bla bla
My problem starts if I want to create 20+ Person objects - I know how to place all the objects in a loop/NSMutableArray as follows:
for (int i = 0; i < 20; i++)
{ Person *player = [[Person alloc] init];
player.age = 10;
player.height = 10;
player.name = @"player";
[myArray addObjectlayer];
[player release];
}
All the objects in myArray are individual but have the same values. Thats fine ! But how to get or change the objects in myArray ? (not at run time)
If [myArray objectAtIndex:4]; holds the following values:
player.age = 10;
player.height = 10;
player.name = @"player";
how do I get to the object(s) in myArray so I can compare/sort/add etc..
Heres example of what I want to do but I know is wrong:
if (player.age == [myArray personAtIndex:15.age]; // wrong !
or
NSLog(@"@ has a height of %i",[myArray personAtIndex:15:name:height];
prints out >> person has a height of 10
I really would appreciate you helping me on this - As a newbie I'm finding it hard to move on until I get an understanding of this problem. Thanks again.