gpt4 book ai didi

ios - 计算器 : Getting runtime app crash when "Enter" button is pressed

转载 作者:行者123 更新时间:2023-12-01 19:22:28 24 4
gpt4 key购买 nike

我编写了一个编译良好的计算器程序,但是当我点击“Enter”按钮时崩溃。

这是崩溃日志:

2012-03-05 14:35:46.561 Calculator[35699:f803] -[CalculatorViewController enterPressed:]: unrecognized selector sent to instance 0x6a590f0
2012-03-05 14:35:46.657 Calculator[35699:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CalculatorViewController enterPressed:]: unrecognized selector sent to instance 0x6a590f0'
*** First throw call stack:
(0x13bb052 0x154cd0a 0x13bcced 0x1321f00 0x1321ce2 0x13bcec9 0x155c2 0x1555a 0xbab76 0xbb03f 0xba2fe 0x3aa30 0x3ac56 0x21384 0x14aa9 0x12a5fa9 0x138f1c5 0x12f4022 0x12f290a 0x12f1db4 0x12f1ccb 0x12a4879 0x12a493e 0x12a9b 0x20fd 0x2065)
terminate called throwing an exception(lldb)

View Controller 的实现:
#import "CalculatorViewController.h"
#import "CalculatorBrain.h"

@interface CalculatorViewController ()
@property (nonatomic) BOOL userIsInTheMiddleOfEnteringANumber;
@property (nonatomic, strong) CalculatorBrain *brain;
@end

@implementation CalculatorViewController
@synthesize display = _display;
@synthesize userIsInTheMiddleOfEnteringANumber = _userIsInTheMiddleOfEnteringANumber;
@synthesize brain = _brain;

- (CalculatorBrain *)brain
{
if(!_brain) _brain = [[CalculatorBrain alloc] init];
return _brain;
}

- (IBAction)digitPressed:(UIButton *)sender
{
NSString *digit = [sender currentTitle];
if (self.userIsInTheMiddleOfEnteringANumber) {
self.display.text = [self.display.text stringByAppendingString:digit];
} else {
self.display.text = digit;
self.userIsInTheMiddleOfEnteringANumber = YES;
}

}

- (IBAction)enterPressed
{
[self.brain pushOperand:[self.display.text doubleValue]];
self.userIsInTheMiddleOfEnteringANumber = NO;
}

- (IBAction)operationPressed:(UIButton *)sender
{
if(self.userIsInTheMiddleOfEnteringANumber) {
[self enterPressed];
}
NSString *operation = sender.currentTitle;
double result = [self.brain performOperation:operation];
self.display.text = [NSString stringWithFormat:@"%g",result];
}

@end

最佳答案

答案在于错误信息-[CalculatorViewController enterPressed:]这表示发送到 View Controller 的选择器应该带有一个参数。但是你已经定义了enterPressed没有任何参数。 enterPressed方法应该更像 operationPressed: IE:

- (IBAction) enterPressed:(UIButton *)sender 

关于ios - 计算器 : Getting runtime app crash when "Enter" button is pressed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9575209/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com