我對Objective-C和iOS非常陌生,所以我一直在玩Picker View。我定義了一個Person類,這樣當你創建一個新Person時,它會自動給這個人一個名字和年齡。使用NSArray初始化對象數組
#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
現在我想打的人的一個數組,所以我可以拋出一堆入選擇器,挑一個人,並顯示他們的年齡。首先,儘管我需要製造一系列人員。我如何創建一個對象數組,初始化和分配它們?
當iiFreeman用正確答案擊敗我時,我已經半途而廢了。但是,我發現nameArr中的第一個名字很棒:D – Bergasms 2013-02-20 00:54:29