I'm learning iOS development through Stanford's iTunesU program. I am stuck on an unexpected problem I am having.
I have added a clear method, but I am getting this error //Use of undeclared identifier 'operandStack'; did you mean '_operandStack'?
I know I can fix the problem by using [self.operandStack ...etc instead of [operandStack
Why do I need self? Isn't it implied? Why do I not need to use self when referencing _operandStack?
#import "CalculatorBrain.h"
@interface CalculatorBrain()
//string because we are the only ones interested
@property (nonatomic, strong) NSMutableArray *operandStack;
@end
@implementation CalculatorBrain
@synthesize operandStack = _operandStack;
- (void) setOperandStack:(NSMutableArray *)operandStack
{
_operandStack = operandStack;
}
- (NSMutableArray *) operandStack
{
if(_operandStack==nil) _operandStack = [[NSMutableArray alloc] init];
return _operandStack;
}
- (void) pushOperand:(double)operand
{
NSNumber *operandObject = [NSNumber numberWithDouble:operand];
[self.operandStack addObject:operandObject];
}
- (double) popOperand
{
NSNumber *operandObject = [self.operandStack lastObject];
if (operandObject !=nil)
{
[self.operandStack removeLastObject];
}
return [operandObject doubleValue];
}
- (void) clear
{
//clear everything
[operandStack removeAllObjects];
//*************************** //Use of undeclared identifier 'operandStack'; did you mean '_operandStack'?
}
- (double) performOperation:(NSString *)operation
{
double result =0;
//calculate result
if ([operation isEqualToString:@"+"]) {
result = [self popOperand] + [self popOperand];
} else if ([operation isEqualToString:@"*"]) {
result = [self popOperand] * [self popOperand];
} else if ([operation isEqualToString:@"π"]) {
[self pushOperand:3.14159];
NSNumber *operandObject = [self.operandStack lastObject];
return [operandObject doubleValue];
}
[self pushOperand:result];
return result;
}
@end