我对 Objective-C 和 iOS 还很陌生,所以我一直在玩 Picker View。我已经定义了一个 Person 类,这样当你创建一个新的 Person 时,它会自动给这个人一个名字和年龄。
#import "Person.h"
@implementation Person
@synthesize personName, age;
-(id)init
{
self = [super init];
if(self)
{
personName = [self randomName];
age = [self randomAge];
}
return self;
}
-(NSString *) randomName
{
NSString* name;
NSArray* nameArr = [NSArray arrayWithObjects: @"Jill Valentine", @"Peter Griffin", @"Meg Griffin", @"Jack Lolwut",
@"Mike Roflcoptor", @"Cindy Woods", @"Jessica Windmill", @"Alexander The Great",
@"Sarah Peterson", @"Scott Scottland", @"Geoff Fanta", @"Amanda Pope", @"Michael Meyers",
@"Richard Biggus", @"Montey Python", @"Mike Wut", @"Fake Person", @"Chair",
nil];
NSUInteger randomIndex = arc4random() % [nameArr count];
name = [nameArr objectAtIndex: randomIndex];
return name;
}
-(NSInteger *) randomAge
{
//lowerBound + arc4random() % (upperBound - lowerBound);
NSInteger* num = (NSInteger*)(1 + arc4random() % (99 - 1));
return num;
}
@end
现在我想创建一个 Persons 数组,这样我就可以将一堆放入选择器中,选择一个 Person 并显示他们的年龄。首先,虽然我需要创建一个 Persons 数组。如何创建对象数组、初始化和分配它们?