我正在寫一個計算器應用程序在objective-c爲iOS。此時我的目標是將用戶按下的數字添加到充當LIFO堆棧的NSMutableArray中,以便稍後可以通過將它們從堆棧彈出來對其執行計算。我在'(IBAction)operationPressed'方法的末尾插入了一個斷點,調試器顯示我的NSMutableArray aka programStack的值爲'nil'。任何人都可以看到我在這裏做錯了嗎?添加對象到NSMutableArray,但得到零而不是
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
你應該你的代碼添加到您的問題,而不是屏幕截圖。你真的分配並初始化你的NSMutableArray? – Paulw11