将对象添加到 NSMutableArray 但得到 nil

adding object to NSMutableArray but get nil instead

我正在 objective-c 中为 iOS 编写一个计算器应用程序。此时我的目标是将用户按下的数字添加到充当 LIFO 堆栈的 NSMutableArray 上,以便稍后我可以通过将它们从堆栈中弹出来对它们执行计算。我在“(IBAction)operationPressed”方法的末尾插入了一个断点,调试器显示我的 NSMutableArray aka programStack 的值为 'nil'。谁能看出我在这里做错了什么?

programStack 在我的 .m 文件中合成并在我的 .h 文件中声明为非原子的,强的

让我知道是否有更多我应该展示的代码

     //All code from my .h file
        #import <UIKit/UIKit.h>
        /*
        @interface ViewController : UIViewController


        @end
        */

        @interface Compute : NSObject
        @property (nonatomic, strong) NSMutableArray *programStack;

        -(void)pushOperand:(double)operand;
        -(double)performOperation:(NSString *)operation;
        -(double)popOperand;
        @end

        @interface Calculator : UIViewController
        @property (nonatomic, strong) Compute *comp;
        @property (nonatomic, weak) NSString *operationUserHasPressed;
        @property (strong, nonatomic) IBOutlet UITextField *display;
        @property double operand;   //I added this

        -(IBAction)digitPressed:(UIButton *)sender;
        -(IBAction)operationPressed:(UIButton *)sender;
        -(IBAction)equalToSignPressed:(UIButton *)sender;
        @end

        //Some code from my .m file
        #import "ViewController.h"
        @interface Compute ()

        @end

        @implementation Compute

        @synthesize programStack;

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

        @end

        @implementation Calculator

        - (void)viewDidLoad {
            [super viewDidLoad];
            // Do any additional setup after loading the view, typically from a nib.
            //An error comes up
            self.programStack = [[NSMutableArray init] alloc];
        }

        - (void)didReceiveMemoryWarning {
            [super didReceiveMemoryWarning];
            // Dispose of any resources that can be recreated.
        }

        @synthesize display;
        @synthesize operationUserHasPressed;
        @synthesize comp;

        -(IBAction)digitPressed:(UIButton *)sender {

            //To obtain the number entered by the user
            NSString *numberEntered = sender.currentTitle;

            //Display number entered by the user
            display.text = numberEntered;

            if ([sender.currentTitle  isEqual: @"1"]) {
                self.operand = 1.00;
            } else if ([sender.currentTitle isEqual:@"2"]) {
                self.operand = 2.00;
            } else if ([sender.currentTitle isEqual:@"3"]) {
                self.operand = 3.00;
            } else if ([sender.currentTitle isEqual:@"4"]) {
                self.operand = 4.00;
            } else if ([sender.currentTitle isEqual:@"5"]) {
                self.operand = 5.00;
            } else if ([sender.currentTitle isEqual:@"6"]) {
                self.operand = 6.00;
            } else if ([sender.currentTitle isEqual:@"7"]) {
                self.operand = 7.00;
            } else if ([sender.currentTitle isEqual:@"8"]) {
                self.operand = 8.00;
            } else if ([sender.currentTitle isEqual:@"9"]) {
                self.operand = 9.00;
            }
        }

        -(IBAction)operationPressed:(UIButton *)sender {

            //To obtain the operation entered by the user
            self.operationUserHasPressed = sender.currentTitle;

            [self.comp pushOperand: self.operand];
        }   //Place breakpoint here

        -(IBAction)equalToSignPressed:(UIButton *)sender {

            //To obtain the result of the computation
            double result = 0;
            [self.comp pushOperand: self.operand];
            result = [self.comp performOperation:self.operationUserHasPressed];

        }
        @end

你说'programStack'是合成的,其实只是说它的publicgetter/setter会生成。它不会为您创建。 正如 Paulw11 在他的评论中指出的那样,我相信您可能错过了分配。

它通常发生在 UIViewController 的 -(id)init 或 -(void)viewDidLoad 中。

- (void)viewDidLoad {
    [super viewDidLoad];

    self.programStack = @[].mutableCopy; // or [[NSMutableArray init] alloc];
}

您需要对代码做一些小的改动。 首先将您的 -(void)viewDidLoad 方法替换为以下内容:

- (void)viewDidLoad {
    [super viewDidLoad];

    // Allocation and Initialization of your Compute Class
    comp = [[Compute alloc] init];
}

并在您的 Compute.m 文件中再添加一种方法:

- (id)init {
    if(self == [super init]) {
        self.programStack = [[NSMutableArray alloc] init];
    }
    return self;
}