不知道如何从Xcode中的类方法访问属性

时间:2012-04-03 17:36:50

标签: objective-c cocoa-touch

我想从类方法storedValue访问属性popOperandOffStack:,但是我收到错误。我读到你无法访问storedValue因为它是类方法或类似的东西。任何人都可以告诉我如何访问storedValue或如何将storedValue设为全局,以便我可以在popOperandOffStack:中使用它?

这是我的标题:

#import <Foundation/Foundation.h>

@interface CalculatorBrain : NSObject

- (void)pushOperand:(double)operand; // - = instance method
- (double)performOperation: (NSString *)operation;

@property (nonatomic,readonly) id program;

+ (double)runProgram:(id)program; // + = class method
+ (NSString *)descriptionOfProgram:(id)program;
@end

这是我的文件:

#import "CalculatorBrain.h"

@interface CalculatorBrain()
@property (nonatomic,strong) NSMutableArray *programStack;
@property (nonatomic) double storedValue;
@end

@implementation CalculatorBrain

@synthesize programStack = _programStack;
@synthesize storedValue = _storedValue;

- (NSMutableArray *)programStack
{
    if (_programStack == nil)
        _programStack = [[NSMutableArray alloc] init];

    return _programStack;
}

- (void)setOperandStack:(NSMutableArray *)programStack
{
    _programStack = programStack;
}

- (void)pushOperand:(double)operand
{
    [self.programStack addObject:[NSNumber numberWithDouble:operand]];
}

- (double)performOperation: (NSString *)operation
{
    [self.programStack addObject:operation];
    return [[self class] runProgram:self.program];
}

- (id)program 
{
    return [self.programStack copy];
}

+ (NSString *)descriptionOfProgram:(id)program
{
    return @"Implement this in Assignment 2";
}

+ (double) popOperandOffStack:(NSMutableArray *)stack
{
    double result = 0;

    id topOfStack = [stack lastObject];
    if (topOfStack) 
        [stack removeLastObject];

    if ([topOfStack isKindOfClass:[NSNumber class]])
    {
        result = [topOfStack doubleValue];
    } 
    else if ([topOfStack isKindOfClass:[NSString class]]) 
    {
        NSString *operation = topOfStack;
        if ([operation isEqualToString:@"+"]){
            result = [self popOperandOffStack:stack] + [self popOperandOffStack:stack];
        } else if ([operation isEqualToString:@"-"]){
            result = -[self popOperandOffStack:stack] + [self popOperandOffStack:stack];
        } else if ([operation isEqualToString:@"*"]){
            result = [self popOperandOffStack:stack] * [self popOperandOffStack:stack];
        } else if ([operation isEqualToString:@"/"]){
            double divide = [self popOperandOffStack:stack];
            if(divide){
                result = [self popOperandOffStack:stack] / divide;
            } else {
                result = 0;
            }
        } else if ([operation isEqualToString:@"1/x"]){
            result = 1 / [self popOperandOffStack:stack];
        } else if ([operation isEqualToString:@"sqrt"]){
            double sqrtNumber = [self popOperandOffStack:stack];
            if (sqrtNumber > 0){
                result = sqrt(sqrtNumber);
            } else {
                result = 0;
            }
        } else if ([operation isEqualToString:@"sin"]){
            result = sin( [self popOperandOffStack:stack]);
        } else if ([operation isEqualToString:@"cos"]){
            result = cos( [self popOperandOffStack:stack]);
        } else if ([operation  isEqualToString:@"MS"]){
            self.storedValue = [self popOperandOffStack:stack];
        } else if ([operation isEqualToString:@"MR"]){
            result = self.storedValue; //error here
        } else if ([operation isEqualToString:@"MC"]){
            result = [self popOperandOffStack:stack];
            self.storedValue = 0; //error here
        } else if ([operation isEqualToString:@"M+"]){
            result = [self popOperandOffStack:stack];
            self.storedValue = self.storedValue + result; //error here
        } else if ([operation isEqualToString:@"M-"]){
            result = [self popOperandOffStack:stack];
            self.storedValue = self.storedValue - result; //error here
        } else if ([operation isEqualToString:@"C"]){
            result = 0;
            while ( [self popOperandOffStack:stack] )
            {
                //clear operands until returns NO
            }
        } else if ([operation isEqualToString:@"CE"]){
            result = 0;
            self.storedValue = 0; //error here
            while ( [self popOperandOffStack:stack] )
            {
                //clear operands until returns NO
            }
        }   

        if (![operation isEqualToString:@"MS"] && ![operation isEqualToString:@"CE"])
        {
            return result;
        } else {
            return self.storedValue;
        }
    }
    return result;
}

+ (double)runProgram:(id)program
{
    NSMutableArray *stack;
    if ([program isKindOfClass:[NSArray class]]) {
        stack = [program mutableCopy];
    }
    return [self popOperandOffStack:stack];
 }

@end

我无法使用self.storedValue。除了将+(静态方法)更改为-(实例方法)之外,我该怎么做(我不允许这样做)?

1 个答案:

答案 0 :(得分:0)

您不能在类方法中使用实例getter / setter方法,因为您没有作为CalculatorBrain实例的对象。 (我没有使用过该课程,但我希望它能解释课程和对象之间的区别。)

尽管从创建面向对象程序的角度来看这不是一个好主意,但可以使用静态变量来保存你的双精度。查看“外部静态”一词的C语言参考,以查看示例。