Peter
Peter

Reputation: 11

Creating 20 objects in a loop

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;
}

Upvotes: 1

Views: 1210

Answers (2)

averydev
averydev

Reputation: 5727

A couple of items.

If I understand your follow up question properly, what you are looking to do is access the objects you have stored in your array so that you can change the values of their properties.

However, the above poster answered the actual question you asked, and you should mark his correct.

If you wanted to go through each item in the array you would do the following:

for (int i=0; i<[players count]; i++) {
Player *aPlayer = [players objectAtIndex:i];
aPlayer.name = @"joe";
}

If you only wanted to access a single player:

Player *aPlayer = [players objectAtIndex:4];
aPlayer.name = @"joe";

Also you may want to customize your Player class and override the description so that you don't have to repeatedly type complex NSLog statements.

-(NSString *)description{
    return [NSString stringWithFormat:@"name = %@ age = %d height = %d", self.name, self.age, self.height];
}

By overriding the description method calling NSLog on your object will return the string from this statement.

Upvotes: 0

viggio24
viggio24

Reputation: 12366

  • Are the objects unique? YES, they are.
  • What the best way to give each object a name,age,height? this question is not clear, so the way you gave an age, height and name to your objects in the loop is correct but of course you're providing the same info to all objects; giving them unique names depends on your application logic, for example you could assign the age randomly in this way:
player.age = arc4random()%90;

You can do the same for the height (eventually with a slightly more complicated formula, e.g. 140+arc4random()%50). Finally for the height you can assign a derived name in this way:

player.name = [NSString stringWithFormat:@"Player-%d",i];

which assigns names Player-0, Player-1, ...

  • Finally to print-out the data in the NSLog:
NSLog(@"Player %d : name=%@ height=%d age=%d",i,player.name,player.height,player.d)

or in a different loop:


int i = 0;
for(Person *player in myArray) {
  NSLog(@"Player %d : name=%@ height=%d age=%d",i,player.name,player.height,player.d);
  i++;
}

Upvotes: 4

Related Questions