我需要一些新手幫助。 所以基本上我試圖創建20個人物(玩家)。每個球員都有一個名字,年齡和身高。循環創建20個對象
而不是寫'Person * player = [[Person alloc] init];''二十次,我做了一個循環。 我認爲循環已經工作,因爲[myArray計數]有20個對象。
我的問題:
是20個對象唯一的(所有相同的名字,年齡,身高)?
什麼是最好的方式給MyArray的每個元素的每個對象的名稱,年齡,身高?
所以我的最終目標是能夠做這樣的事情:
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);
等等
希望以上是有道理的,我真的很感謝你的幫助。
#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;
}
這些個人名稱,年齡,高度從何而來? – 2011-09-22 11:55:53