我正在通过斯坦福大学的iTunesU程序学习iOS开发。我遇到了一个意想不到的问题。为什么我必须使用自我。以引用自己的对象
我已经添加了一个明确的方法,但我得到这个错误 //使用未声明的标识符'operandStack';你的意思是'_operandStack'?
我知道我可以用[self.operandStack解决问题...等,而不是[operandStack
为什么需要自我?这不是暗示吗?为什么在引用_operandStack时不需要使用self?
#进口 “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];
// * ** * ** * ** * ** * ** * * * * ** * ** * ** //使用未声明的标识符'operandStack';你的意思是'_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
继续操作前再次看课。他们对此非常清楚,你应该掌握setter/getters的概念与实例变量,并让自己适应整个课程中始终使用的懒惰实例。 – Mario 2013-04-04 19:27:17
懒惰的实例化非常简单。我非常了解getter和setter以及实例变量。我所理解的是,我们需要在引用实例方法或属性时使用self,并且我们不需要为_operandStack使用self。在我的C#/ .net背景中,它总是指向当前实例,并可用于访问实例中可访问的任何对象。 – user1060500 2013-04-08 15:49:44