Question

I am making a simple calculator application that displays the equation that the user has inputed and also the output of the equation. At the top of my header file I created three integers and an array...

#import <UIKit/UIKit.h>

int Method;
int SelectNumber;
float RunningTotal;

NSMutableArray *Equation;

@interface SecondCalculatorViewController : UIViewController{

IBOutlet UILabel *EquationLabel;
IBOutlet UILabel *ResultLabel;

}

- (IBAction)OneButton:(id)sender;
- (IBAction)TwoButton:(id)sender;
- (IBAction)ThreeButton:(id)sender;
- (IBAction)FourButton:(id)sender;
- (IBAction)FiveButton:(id)sender;
- (IBAction)SixButton:(id)sender;
- (IBAction)SevenButton:(id)sender;
- (IBAction)EightButton:(id)sender;
- (IBAction)NineButton:(id)sender; 
- (IBAction)ZeroButton:(id)sender;

- (IBAction)Plusbutton:(id)sender;
- (IBAction)MinusButton:(id)sender;
- (IBAction)MultiplyButton:(id)sender;
- (IBAction)DivideButton:(id)sender;

- (IBAction)CalculateButton:(id)sender;
- (IBAction)ClearButton:(id)sender;

@end

In my implementation, I prepare each input to be displayed in a label and then also I try to save SelectNumber into the first index in the array I created. I then try to display it in the label. Here is an example of one of the buttons...

- (IBAction)OneButton:(id)sender {

//[Equation addObject:0];

SelectNumber = SelectNumber * 10;
SelectNumber = SelectNumber + 1;
//EquationLabel.text = [NSString stringWithFormat:@"%i",SelectNumber];
[Equation replaceObjectAtIndex:0 withObject:[NSString stringWithFormat:@"%i",SelectNumber]];
EquationLabel.text = [Equation objectAtIndex:0];
}

When I run this in the simulator and push any number, it does not display the first value in the array "Equation".

How can I fix this?

Was it helpful?

Solution

It seems that Equation is not initialized. You need to initialize it:

Equation = [NSMutableArray new];
// or Equation = [[NSMutableArray alloc] init];

viewDidLoad method could be a good place for this code in your case.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top