2011-11-26 29 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