Here’s the code
In the .h…
@interface Calculator2ViewController : UIViewController {
NSString *currentNumber;
bool buildingNumber;
NSMutableArray *arrayExpression;
IBOutlet UILabel *display;
}
In the .m…
-(NSString *) makeCurrentNumber{
if(!currentNumber){
currentNumber = [[NSString alloc] init];
currentNumber = @"";
}
NSLog(@"Inside make makeCurrentNumber. The currentNumber is %@",currentNumber);
return currentNumber;
}
-(IBAction) digitPressed: (UIButton *)sender{
NSString *digit = sender.titleLabel.text;
NSLog(@"The digit is: %@", digit);
if(!currentNumber)
currentNumber = @"";
currentNumber = [currentNumber stringByAppendingString:digit];
NSLog (@"The string is: %@",currentNumber);
buildingNumber = YES;
}//end of digitPressed
Why does my program crash after 3 appends?
currentNumber initially is retained by the alloc-init in makeCurrentNumber. Later on, you are replacing its value with a auto retained value (the result from
stringByAppendingString. Enhance that line with a retain and you might get better results.