据我了解,self 指的是当前类,当我在 self 之后使用一个点时,就是使用它的一个属性。在这里的代码中有一个用途self.popOperand
,我不明白 popOperanad 是否不是属性。我不明白的另一件事是为什么
[self pushOperand:result];
有效而[self.pushOperand:result];
无效。
#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