所以我正在开发一个计算器应用程序,我的代码目前没有正常执行。我知道出了什么问题,但我不确定如何解决它。目前,当我按下添加按钮时,我的ViewController.m包含当前代码:
#import "ViewController.h"
#import "CalcLogic.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *display;
@property (weak, nonatomic) IBOutlet UILabel *lastOperation;
@property (strong, nonatomic) CalcLogic* logic;
@end
@implementation ViewController
double result = 0;
//Last operation entered into the calculator
NSString* lastEntered;
@synthesize logic;
-(IBAction)numPressed:(UIButton *)sender{
BOOL hasBeenCleared = [self.lastOperation.text isEqualToString:@"Clear"];
if ([self.display.text isEqualToString:@"0."]) {
self.display.text = sender.currentTitle;;
self.lastOperation.text = sender.currentTitle;;
[self.logic pushNumber:[sender.currentTitle doubleValue]];
}
else{
self.display.text = [self.display.text stringByAppendingString:sender.currentTitle];
if (self.lastOperation.text.length > 1 && hasBeenCleared != TRUE) {
self.lastOperation.text = [self.lastOperation.text stringByAppendingString:sender.currentTitle];
}
else {
self.lastOperation.text = sender.currentTitle;
}
[self.logic pushNumber:[sender.currentTitle doubleValue]];
}
}
-(IBAction)clearPressed:(UIButton *)sender{
self.display.text = @"0.";
self.lastOperation.text = @"Clear";
[self.logic clearStack];
result = 0;
}
-(IBAction)operation:(UIButton *)sender{
[logic pushOperation:sender.currentTitle];
NSString* resultString = [NSString stringWithFormat:@"%g", result];
self.display.text = resultString;
if ([self.lastOperation.text isEqualToString:@"Clear"]) {
self.lastOperation.text = @"";
self.lastOperation.text = [self.lastOperation.text stringByAppendingString:sender.currentTitle];
}
else{
self.lastOperation.text = [self.lastOperation.text stringByAppendingString:sender.currentTitle];
}
}
-(IBAction)equalHit:(UIButton *)sender{
result = [self.logic performOperation];
self.display.text = [NSString stringWithFormat:@"%g", result];
}
我的问题是将对象推送和弹出到数组。数组位于logic
,我试图将数字推送到logic
中的两个数组之一,并将运算符推送到对象中的另一个数组。但是,我必须做错事,因为当我检查控制台时没有任何东西被推(据我所知)。我还是这门语言的新手,来自Java包装。
答案 0 :(得分:1)
看起来您没有在代码中的任何位置分配/ init logic
。您需要这一行,可能在viewDidLoad
或其他一些初始化函数中:
logic = [[CalcLogic alloc] init];