0

簡単な一問一答のiPhoneアプリを作っています。

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    // Call the init method implemented by the superclass
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Create two arrays and make the pointers point to them
        questions = [[NSMutableArray alloc] init];
        answers = [[NSMutableArray alloc] init];
        // Add questions and answers to the arrays
        [questions addObject:@"What is 7 + 7?"];
        [answers addObject:@"14"];
        [questions addObject:@"What is the capital of Vermont?"];
        [answers addObject:@"Montpelier"];
        [questions addObject:@"From what is cognac made?"];
        [answers addObject:@"Grapes"];
    }

配列の質問と回答が満たされておらず、nil が表示されていません。何が問題なのですか?

配列アクセス方法:

- (IBAction)showQuestion:(id)sender
{
// Step to the next question
currentQuestionIndex++;
// Am I past the last question?
if (currentQuestionIndex == [questions count]) {
    // Go back to the first question
    currentQuestionIndex = 0;
}

// Get the string at that index in the questions array
NSString *question = [questions objectAtIndex:currentQuestionIndex]; **<<- question is nil**


[questionField setText:question];

// Clear the answer field
[answerField setText:@"???"];
}
4

1 に答える 1

0

あなたの答えは、次の方法で NSMUtableArrays を開始することです。

questions = [[NSMutableArray alloc] initWithCapacity:1];

更新: コードが IF 条件内に到達しない場合、それは非常に奇妙な状況です。現在の XIB を削除し、新しい XIB をゼロから作り直すことをお勧めします。このような奇妙なバグは、XIB を再作成することで解決されることがあります。

于 2012-06-19T11:59:35.837 に答える