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"];
    }

数组问题和答案没有被填满并显示为零。可能是什么问题?

数组访问方法:

- (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 可以解决此类奇怪的错误。

于 2012-06-19T11:59:35.837 回答