I need some newbie help. So basically im trying create 20 individual objects (players). Each player has a name , age and height.
Instead of writing 'Person *player = [[Person alloc] init];' Twenty times, I have made a loop. I think the loop has worked because the [myArray count] has 20 objects.
My questions:
Are the 20 objects unique ( all with same name, age, height) ?
Whats the best way to give each object in each element of MyArray a name,age,height ?
So my end goal is to be able to do something like this:
NSLog(@"%@ is %i high and is %i years old", player1.name, player1.height, player1.age);
NSLog(@"%@ is %i high and is %i years old", player2.name, player2.height, player2.age);
etc...
I hope the above makes sense and I really appreciate your help.
#import <Foundation/Foundation.h>
#import "Person.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSMutableArray *myArray = [[NSMutableArray alloc]initWithCapacity:20];
for (int i = 0; i < 20; i++)
{
Person *player = [[Person alloc] init];
player.age = 10;
player.height = 10;
player.name = @"player";
[myArray addObject:player];
[player release];
}
NSLog(@"The number of players in myArray = %i", [myArray count]); // I now have 20 players (objects) in myArray.
//How can I now give each player object an individual name, age & height ??
[pool drain];
return 0;
}