user1060500
user1060500

Reputation: 1535

Why do I have to use self. to reference own object

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

Upvotes: 0

Views: 148

Answers (1)

Ramy Al Zuhouri
Ramy Al Zuhouri

Reputation: 21966

Because you have synthesized it (note: in newer Objective-C version the synthesis is automatic) :

@synthesize operandStack = _operandStack;

It means that you generated getter and setter, and that you access the property by calling it _operandStack. If you want to call it operantStack change it to that:

@synthesize operandStack;

If instead you use self.operandStack, you are using the getter/setter generated by the property, not the synthesized one.

Using synthesized and not synthesized properties is different, there isn't a "recommended way" of accessing properties like many people think, they just have different meaning. For example here:

- (void) setOperandStack:(NSMutableArray *)operandStack
{
    _operandStack = operandStack;
}

You must use the synthesized property, otherwise you go into an infinite loop. The synthesized property is automatically generated, the non synthesized property is also automatically generated but it can be overridden, like you did in that case, and it can be also accessed externally.

Upvotes: 4

Related Questions