title: Objective C Calculator Programming Variable
body: <p>Okay, so I've been working on the Stanford iOS development course that they have posted for free online. I've been working on figuring out how to make a programable variable. It has been working fine so far, aside from the fact that I think the following lines of code are programming the variable to be @"x=" instead of the previous number entered.</p>
  
  <p>The View Controller:</p>
  
  <pre><code>#import "ViewController.h"
  #import "CalculatorBrain.h"
  
  @interface ViewController()
  @property (nonatomic) BOOL userIsInTheMiddleOfEnteringANumber;
  @property (nonatomic) BOOL userPressedSomethingElse;
  @property (nonatomic, strong) CalculatorBrain *brain;
  @end
  
  @implementation ViewController
  @synthesize display;
  @synthesize inputHistory;
  @synthesize userPressedSomethingElse;
  @synthesize userIsInTheMiddleOfEnteringANumber;
  @synthesize brain = _brain;
  
  - (CalculatorBrain *)brain
  {
      if (!_brain) _brain = [[CalculatorBrain alloc] init];
      return _brain;
  }
  
  NSString *xValue = @"0";
  
  - (IBAction)enterPressed 
  // A specific action if enter is pressed
  {
      [self.brain pushOperand:[self.display.text doubleValue]];
      self.userIsInTheMiddleOfEnteringANumber = NO;
      if (self.userPressedSomethingElse) 
      {
          self.inputHistory.text = [self.inputHistory.text stringByAppendingString:@" "];
      }
      self.userPressedSomethingElse = NO;
  }
  
  - (IBAction)variableChanged:(id)sender 
  {
      if (self.userIsInTheMiddleOfEnteringANumber) 
      {
          [self enterPressed];
      }
      NSString *operation = [sender currentTitle];
      xValue = [self.brain programVariable:operation];
      self.inputHistory.text = [self.inputHistory.text stringByAppendingString:@"X="];
      self.inputHistory.text = [self.inputHistory.text stringByAppendingString:xValue];
  }
  </code></pre>
  
  <p>The Calculator Brain (the .m one):</p>
  
  <pre><code>#import "CalculatorBrain.h"
  
  @interface CalculatorBrain()
  @property (nonatomic,strong) NSMutableArray *operandStack;
  @end
  
  @implementation CalculatorBrain
  
  @synthesize operandStack = _operandStack;
  - (NSMutableArray *) operandStack
  {
      if (!_operandStack) 
      {
          _operandStack = [[NSMutableArray alloc] init];
      }
      return _operandStack;
  }
  
  - (void) pushOperand:(double)operand
  {
      NSNumber *operandObject = [NSNumber numberWithDouble:operand];
      [self.operandStack addObject:operandObject];
  }
  
  - (double)popOperand
  {
      NSNumber *operandObject = [self.operandStack lastObject];
      if (operandObject) [self.operandStack removeLastObject];
      return [operandObject doubleValue];
  }
  
  - (NSString *) programVariable: (NSString *) operation
  {
      double result = [self popOperand];
      NSString *resultString = [NSString stringWithFormat:@"%.2d",result];
      return resultString;
  }
  </code></pre>
  
  <p>The .h Calculator Brain:</p>
  
  <pre><code>#import &lt;Foundation/Foundation.h&gt;
  
  @interface CalculatorBrain : NSObject
  
  - (void) pushOperand: (double) operand;
  - (double) performOperation: (NSString *) operation;
  - (NSString *) programVariable: (NSString *) operation;
  
  @end
  </code></pre>
  
  <p>The button that is pushed says "x=", and because of some tracing statements I added, I have figured out that this is being set to xValue. However, I don't know how to fix it... Any ideas?</p>