【发布时间】:2011-09-22 11:54:37
【问题描述】:
我需要一些新手帮助。 所以基本上我正在尝试创建 20 个单独的对象(玩家)。每个玩家都有姓名、年龄和身高。
而不是写'Person *player = [[Person alloc] init];'二十次,我做了一个循环。 我认为循环有效,因为 [myArray count] 有 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;
}
【问题讨论】:
-
这些个人姓名、年龄、身高从何而来?
标签: objective-c