Help with Obj C - creating multiple objects

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.
 
OP
C
Joined
Sep 22, 2011
Messages
2
Reaction score
0
Points
1
Hi Nethfel
Yes that was me being rather impatient and desperate last night for an answer.
When I wrote this post I hadnt realised that you had sent a 2nd answer on the newNoston forum that really solved my query.

I have replied to you at newBoston and again, thank you for all your help.
 
Joined
Feb 25, 2009
Messages
2,112
Reaction score
71
Points
48
Your Mac's Specs
Late 2013 rMBP, i7, 750m gpu, OSX versions 10.9.3, 10.10
No worries, just kinda funny sometimes :)
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top