0

I am creating simle question-answer iPhone app.

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

The arrays questions & answers are not getting filled and showing nil. What might be the problem?

array access method:

- (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:@"???"];
}
Tim
  • 1,659
  • 1
  • 21
  • 33
ExploringApple
  • 1,348
  • 2
  • 17
  • 30
  • Where do you declare your questions and answers array objects? Could you paste the code you're using for this? – Tim Jun 19 '12 at 05:55
  • in header file: `@interface QuizViewController : UIViewController { int currentQuestionIndex; // The model objects NSMutableArray *questions; NSMutableArray *answers; // The view objects - don't worry about IBOutlet - // we'll talk about it shortly IBOutlet UILabel *questionField; IBOutlet UILabel *answerField; }` – ExploringApple Jun 19 '12 at 06:26
  • try to put a breakpoint into the -initWithNibName method. See if it gets there. My guess is that you're not initializing the ViewController by this method, maybe just by -init. – Adrian Ancuta Jun 19 '12 at 11:28
  • My guess is that the problem is not in your code, but in your XIBs. Could you perform the checks described here: http://stackoverflow.com/a/10771281/521923? – Tim Jun 19 '12 at 12:48
  • @Tim checked everything is correct for XIB. – ExploringApple Jun 19 '12 at 13:11

1 Answers1

0

Your answer is to initiate your NSMUtableArrays in the following manner:

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

UPDATE: If your code doesn't reach inside IF condition, it is a very strange situation. I propose you to remove your current XIB and remake a new one, from scratch. Sometimes, strange bugs like this are being solved by recreating the XIB.

Oleg Danu
  • 4,149
  • 4
  • 29
  • 47
  • Here the problem is control is not going inside the if loop i.e. some how 'self' is getting nil. While I am debugging it the control is itself is not going inside function initWithNibName. What might be the reason? If I take the above code and keep in other function like ShowQuestion the array is filling up properly. – ExploringApple Jun 19 '12 at 12:30
  • It also works with [[NSMutableArray alloc] init], this is not the problem, I think. – Tim Jun 19 '12 at 12:46
  • @Tim, yes that is not problem. The actual problem is either control is not going in initWithNibName or it is going inside but not in if condition.(Breakpoint is not hitting in function initWithNibName) – ExploringApple Jun 19 '12 at 12:59
  • Please see my comment beneath your question: Could you check your XIB? – Tim Jun 19 '12 at 13:08
  • @Tim XIB is correct. Point to note that if i place the above code in other function it works fine. So that might be not problem. – ExploringApple Jun 19 '12 at 13:20