2011-11-26 30 views
0

在我的模型实现文件中出现了两个错误,我已经注释到了这两个错误。你能解释什么是错的,以及如何解决它?模型中的预期标识符

谢谢。


CalculatorBrain.m

#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)setOperandStack:(NSMutableArray *)anArray 
{ 
    _operandStack = anArray; 
} 

- (void)pushOperand:(double)operand 
{ 
    [NSNumber *operandObject = [NSNumber numberWithDouble:operand]; // Expected  identifier 
    [self.operandStack addObject:operandObject]; /* Use of undeclared identifier  'operandObject' */ 
} 

- (double)popOperand 
     { 
     NSNumber *operandObject = [self.operandStack lastObject]; 
     return [operandObject doubleValue]; 
     } 

- (double)performOperation:(NSString *)operation 
{ 
    double result = 0; 

    if ([operation isEqualToString:@"+"]) { 
     result = [self popOperand] + [self popOperand]; 
    } else if ([@"*" isEqualToString:operation]) { 
     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 

回答

2

你有一个流浪[

[NSNumber *operandObject = [NSNumber numberWithDouble:operand]; 
^ 
+0

谢谢你,修好了! – pdenlinger

1

您有一个额外的 '[' 在这里:

[NSNumber *operandObject = [NSNumber numberWithDouble:operand]; 

应该是:

NSNumber *operandObject = [NSNumber numberWithDouble:operand]; 
0

正如我以前的人已经指出,你有一个额外的[浮动。另外,你们为什么使用@synthesize实现operandStack变量的getter和setter?

+0

使用@synthesize然后实现operandStack的getter和setter作为练习。 (此练习是斯坦福大学2011年秋季iPhone编程课程的一部分。) – pdenlinger