私はいくつかの iOS の例をオンラインで調べてきましたが、アプリが配列を初期化し、iOS アプリの起動時にオブジェクトを追加するアプリに出くわしました。実装ファイルで使用すると初期化(void) viewDidLoad
が機能しますが、使用すると配列の初期化が機能しません
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
なぜそうなのか誰か教えてもらえますか?ありがとう!
ここにコードがあります -
(void) viewDidLoad
{
if (self) {
questions = [[NSMutableArray alloc] init];
answers = [[NSMutableArray alloc] init];
// Add objects to the arrays
[questions addObject:@"What is 1+1"];
[answers addObject:@"2"];
[questions addObject:@"What is 2+2"];
[answers addObject:@"4"];
[questions addObject:@"What is 3+3"];
[answers addObject:@"6"];
}
[super viewDidLoad];
initWithNibName のコード
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Create two arrays and make the pointers point to them
questions = [NSMutableArray array];
answers = [NSMutableArray array];
// Add questions and answers to the arrays
[questions addObject:@"From what is cognac made?"];
[answers addObject:@"Grapes"];
[questions addObject:@"What is 7 + 7?"];
[answers addObject:@"14"];
[questions addObject:@"What is the capital of Vermont?"];
[answers addObject:@"Montpelier"];
}
return self;
}