我正在创建simle问答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:@"???"];
}
答案 0 :(得分:0)
您的答案是以下列方式启动NSMUtableArrays:
questions = [[NSMutableArray alloc] initWithCapacity:1];
<强>更新强> 如果您的代码没有达到IF内部条件,那么这是一个非常奇怪的情况。我建议你从头开始删除你当前的XIB并重新制作一个新的XIB。有时,像这样的奇怪错误正在通过重新创建XIB来解决。