我是objectiv-c的新手,并试图通过这个讲座来学习它 - 来自斯坦福的播客:http://www.stanford.edu/class/cs193p/cgi-bin/drupal/ 非常好的事情,但我在构建rpn计算器时遇到了问题。当我使用popOperand时它会崩溃。
这是我的模特:
#import "CalculatorBrain.h"
@interface CalculatorBrain()
@property (nonatomic,strong) NSMutableArray *operandStack;
@end
@implementation CalculatorBrain
@synthesize operandStack = _operandStack;
-(NSMutableArray *)operandStack {
if(_operandStack == nil) {
_operandStack = [[NSMutableArray alloc] init];
NSLog(@"zing");
};
return _operandStack;
}
- (void) pushOperand:(double)operand {
NSLog([[self.operandStack lastObject] stringValue] );
[self.operandStack addObject:[NSNumber numberWithDouble:operand]];
NSLog(@"----");
NSLog([[self.operandStack lastObject] stringValue] );
};
-(double) popOperand {
NSNumber* operandObject = [self.operandStack lastObject];
if(operandObject) [self.operandStack removeLastObject];
return [operandObject doubleValue];
}
- (double) performOperation:(NSString *)operation{
double result = 0;
if([operation isEqualToString:@"+"]) {
result = [self popOperand] + [self popOperand];
} else if([operation isEqualToString:@"*"]) {
result = [self popOperand] * [self popOperand];
} else if([operation isEqualToString:@"-"]) {
double subtrahend = [self popOperand];
result = [self popOperand] - subtrahend;
} else if([operation isEqualToString:@"/"]) {
double divisor = [self popOperand];
if(divisor) result = [self popOperand] / divisor;
}
[self pushOperand:result];
return result;
};
@end
真的不知道怎么回事。操作数在堆栈上,但似乎popOperand无法访问?!
答案 0 :(得分:0)