我无法在 NSMutableArray 上推送内容
请看附上的代码
#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
动作是这样调用的:
- (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;
}
我使用这个程序得到的输出是
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
我也尝试过 lastObject ,但它没有用。
在这里编辑 ----------
我试着添加这个
-(CalculatorBrain *)Brain{
if(self.Brain == NULL){
self.Brain = [[CalculatorBrain alloc] init];
}
return self.Brain;
}
现在我的输出只是
2012-08-01 21:03:00.282 Calculator[13706:c07] enter pressed....Number is 23
之后整个应用程序崩溃
谢谢。我在 xcode 和 lion 上看到了相同的程序。