当前位置: 动力学知识库 > 问答 > 编程问答 >

ios - adding object to NSMutableArray but get nil instead

问题描述:

I'm writing a calculator app in objective-c for iOS. My goal at this point is to add the digit that the user presses onto an NSMutableArray acting as a LIFO stack so I can later perform calculations on them by popping them off the stack. I inserted a breakpoint at the end of the '(IBAction)operationPressed' method and the debugger shows that my NSMutableArray aka programStack has the value 'nil'. Could anyone see what I am doing wrong here?

programStack is synthesized in my .m file and declared as nonatomic, strong in my .h file

Let me know if there is more code that I should be showing

 //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

网友答案:

You need to do some minor changes with your code. First of all replace your -(void)viewDidLoad method with following :

- (void)viewDidLoad {
    [super viewDidLoad];

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

And add one more method in your Compute.m File:

- (id)init {
    if(self == [super init]) {
        self.programStack = [[NSMutableArray alloc] init];
    }
    return self;
}
网友答案:

You mention that 'programStack' is synthesized, but it only means that its public getter/setter will be generated. It will not be created for you. As Paulw11 noted in his comment, I believe you probably miss the allocation.

It would usually take place in the -(id)init or the -(void)viewDidLoad of your UIViewController.

- (void)viewDidLoad {
    [super viewDidLoad];

    self.programStack = @[].mutableCopy; // or [[NSMutableArray init] alloc];
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: