iOS上的RPN计算器 - 无法弄清楚崩溃的原因

时间:2011-12-12 18:54:11

标签: iphone objective-c ios ios5

我是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无法访问?!

1 个答案:

答案 0 :(得分:0)

哦,伙计们,哈哈。抱歉,完全是我的错:没看到我打开断点 - 所以应用程序停止是绝对正确的。我想我需要睡一觉