Note that there are some explanatory texts on larger screens.

plurals
  1. POCalculator app won't add operand to array
    primarykey
    data
    text
    <p>I am trying to make a calculator app, but when I press enter nothing is pushed into the array. I have a class called <code>CaculatorBrain</code> where the <code>pushElement</code> method is defined, however (for now) I defined and implemented <code>pushElement</code> method in the view controller. </p> <p>When I log the operand object as it is typed in the console when enter is pressed the contents of array is nil! Why is that?</p> <pre><code>#import "CalculatorViewController.h" #import "CalculatorBrain.h" @interface CalculatorViewController () @property (nonatomic)BOOL userIntheMiddleOfEnteringText; @property(nonatomic,copy) NSMutableArray* operandStack; @end @implementation CalculatorViewController BOOL userIntheMiddleOfEnteringText; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } -(NSMutableArray*) operandStack { if (_operandStack==nil) { _operandStack=[[NSMutableArray alloc]init]; } return _operandStack; } -(CalculatorBrain*)Brain { if (!_Brain) _Brain= [[CalculatorBrain alloc]init]; return _Brain; } - (IBAction)digitPressed:(UIButton*)sender { if (self.userIntheMiddleOfEnteringText) { NSString *digit= [sender currentTitle]; NSString *currentDisplayText=self.display.text; NSString *newDisplayText= [currentDisplayText stringByAppendingString:digit]; self.display.text=newDisplayText; NSLog(@"IAm in digitPressed method"); } else { NSString *digit=[sender currentTitle]; self.display.text = digit; self. userIntheMiddleOfEnteringText=YES; } } -(void)pushElement:(double)operand { NSNumber *operandObject=[NSNumber numberWithDouble:operand]; [_operandStack addObject:operandObject]; NSLog(@"operandObject is %@",operandObject); NSLog(@"array contents is %@",_operandStack); } - (IBAction)enterPressed { [self pushElement: [self.display.text doubleValue] ]; NSLog(@"the contents of array is %@",_operandStack); userIntheMiddleOfEnteringText= NO; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload