0

I am not able to push stuff on NSMutableArray

Please see the code attached

#import "CalculatorBrain.h"

@implementation CalculatorBrain

@synthesize operandStack = _operandStack;

-(NSMutableArray*) operandStack{
    if(self.operandStack == NULL){
        self.operandStack = [[NSMutableArray alloc] init];
    }
    return self.operandStack;
}

-(void)setOperandStack:(NSMutableArray *)operandStack{
    _operandStack = operandStack;
}

-(void) pushOperand:(double) operand{
    [self.operandStack addObject:[NSNumber numberWithDouble:operand]];
    int count = [self.operandStack count];
    NSLog(@"Number pushed is %g", [[self.operandStack objectAtIndex:count - 1] doubleValue]);
}

-(double)popOperand{
    int count = [self.operandStack count];
    NSNumber* value = [self.operandStack objectAtIndex:count - 1];
    double val = 0;
    if(value){
        val = [value doubleValue];
        [self.operandStack removeLastObject];
    }
    NSLog(@"popped %g",val);
    return val;
}
@end

The action is called like this:

- (IBAction)enterPressed {
    double mynumber= [self.display.text doubleValue];
    NSLog(@"enter pressed....Number is %g", mynumber);
    [self.Brain pushOperand:mynumber];
    int count = [self.Brain.operandStack count];
    NSLog(@"enter pressed... pusehed operand is %g", [[self.Brain.operandStack objectAtIndex:count - 1] doubleValue]);
    self.amIInMiddleOfNumber = NO;
}

Output i am getting using this program is

 2012-07-31 22:30:53.838 Calculator[13408:c07] enter pressed....Number is 45
 2012-07-31 22:30:53.840 Calculator[13408:c07] enter pressed... pusehed operand is 0

I have tried lastObject too and it didn't work.

EDIT HERE ----------

I tried adding this

  -(CalculatorBrain *)Brain{
      if(self.Brain == NULL){
          self.Brain = [[CalculatorBrain alloc] init];
      }
      return self.Brain;
  }

And now my output is just

  2012-08-01 21:03:00.282 Calculator[13706:c07] enter pressed....Number is 23

and whole app crashes after that

Thanks. I saw the same program working on xcode and lion.

bruceparker
  • 1,235
  • 1
  • 17
  • 33
  • The `self.operandStack` references in `operandStack` look odd...normally the syntax `self.operandStack` (without `=`) *means* `[self operandStack]` so it's as if the code is calling itself. What if you change the references in the `operandStack` method to use the instance variable `_operandStack` directly? – Kevin Grant Aug 01 '12 at 06:03
  • Also, `%g` expects a float, but you're passing a double. –  Aug 01 '12 at 06:09
  • 1
    self.Brain will be nil. The problem is not in this code. Where is self.Brain set up? – jrturton Aug 01 '12 at 06:15
  • Yes, I agree with @jtrturton - self.Brain is nil. Note that you should not name ivars/properties with a capital letter as this totally messes up the heuristic apple uses to create setters - which is with property foo, you get a synthesized getter of "- ()foo {..." and -(void)setFoo:... – David H Aug 01 '12 at 11:49
  • @synthesize Brain = _brain; I do something like this. I will try to make a setter something like this -(Brain*) Brain{ if(self.Brain == NULL) self.Brain = [[Brain alloc] init]; return self.Brain ; } – bruceparker Aug 01 '12 at 18:32
  • i just edited my question after doing what you said. – bruceparker Aug 02 '12 at 04:07
  • Also, %g prints double accord to http://cocoadev.com/wiki/NSLog and also ,y program prints the first statement. IF %g was the issue, it wouldnt even print once. – bruceparker Aug 02 '12 at 04:32

1 Answers1

0

Your accessor method for Brain is creating an infinite loop. self.Brain calls this method, and you call it from within itself.

First, refactor the property so it is called brain. Properties and instance variables in Objective-C begin with a lower case letter.

Synthesize like so:

@synthesize brain = _brain;

This creates a backing instance variable called _brain which holds the value of your property. (Note that this is now done automatically in Xcode 4.4, but we'll leave it here for instruction).

Make your accessor method like this. The principal difference to your current implementation is that you are using the backing ivar and not recursively calling the property:

-(CalculatorBrain *)brain{
    if(!_brain){
        self.brain = [[CalculatorBrain alloc] init];
    }
    return _brain;
}

Note that setting the value using the property is fine, because that is calling setBrain, and ensures that anything in a custom setter is being called.

jrturton
  • 118,105
  • 32
  • 252
  • 268
  • I tried this and it didnt work. I did also same for operandStack but didn't solve but yes atleast my code doesnt hang anymore. Can i send you my files over email or something. I know that's not the way to do it but still, no harm in asking. Once we figure out the solution, you can post the answer here and i can tick on it so that everyone else also knows the answer – bruceparker Aug 02 '12 at 20:20
  • You could put your project on GitHub or something and link to it in your question. It's hard to believe that an OS update has caused this problem. – jrturton Aug 02 '12 at 21:06
  • "didn't work" is pretty vague as well. What logs are you seeing now? Have you used the debugger to step though your code? That would make it pretty obvious where the problem is. – jrturton Aug 02 '12 at 21:07