To my understand self refers to the current class and when i use a dot after self is to use one of its properties. In the code here there’s a use in self.popOperand that i don’t understand if popOpernad is not a property. Another thing i don’t understand is why
[self pushOperand:result]; works and [self.pushOperand:result]; doesn’t.
#import "Calcbrain.h"
@interface Calcbrain()
@property (nonatomic,strong) NSMutableArray *operandStack;
@end
@implementation Calcbrain
@synthesize operandStack = _operandStack;
-(NSMutableArray *) operandStack
{
if(_operandStack == nil) _operandStack = [[NSMutableArray alloc]init];
return _operandStack;
}
-(double)popOperand
{
NSNumber *objectNum = [self.operandStack lastObject];
if (objectNum)[self.operandStack removeLastObject];
return [objectNum doubleValue];
}
/*-(void) setOperandStack:(NSMutableArray *)operandStack
{
_operandStack = operandStack;
}*/
-(void)pushOperand:(double)opernand
{
[self.operandStack addObject:[NSNumber numberWithDouble:opernand]];
}
-(double)performOperation:(NSString *)operation
{
double result=0;
if([operation isEqualToString:@"+"])
{
result = self.popOperand + self.popOperand;
}
else if ([@"*" isEqualToString:operation])
{
result = self.popOperand * self.popOperand;
}
[self pushOperand:result];
return result;
}
@end
Whilst the
.notation is primarily used for properties, it can be used for paramaterless methods that return a value. Why? Because the synthesised getter for a property is in the same form.Is equivalent to the property declaration:
Whilst there may be no property declaration, it doesn’t mean the
.notation cannot be used. The compiler will effectively change.notation to a method call when compiling, as.is a form of syntactic sugar. As so:This leads on to part 2, why does
[self.pushOperand:result];not work? The reason being is that.does not support the passing of parameters directly.The only way to assign/push a parameter to a property is via
self.pushOperand = result, but this wouldn’t work, because there isn’t a corresponding- (void)setPushOperand:(double)pushOperand;that the.notation assignment maps to.[self pushOperand:result];works because you’re being explicit in calling a particular method, calledpushOperand:.Overall, keep
.notation for properties only, and if you’re using a method that isn’t designed to be a ‘property’, be explicit.Update:
selfis a reserved keyword, that represents a pointer to the instance we’re working within at that time.For example, I can create two instances of
Calcbrainoutside of Calcbrain, for example BrainViewController:Now,
Calcbrainhas methods declared within it, let’s use-(double)performOperation:(NSString *)operationas an example. Now, if I wanted to call that from BrainViewController, I would do:Because we are calling a method which is part of another class, I have to determine the correct instance I’ve created to refer to it (i.e.
instance1andinstance2). But how would I call that from within the class itself, and make sure it applies to the correct instance? The instance I’ve created is unaware of the other instances I’ve created. Useself.selfallows you to reference yourself within methods. So if I wanted toperformOperationwithinCalcbrainitself, I would need to use self: