0

我对 Objective-C 比较陌生,所以我对我正在编码的所有内容都不是 100%。但是,我正在处理发生的错误,并且我遇到了一个运行时错误,我不确定如何修复。该错误声称“未识别的选择器 [已] 发送到实例。”

- (IBAction) equalsPressed{
    self.userIsInTheMiddleOfTypingANumber = NO;
    if (self.brain.operationIsPicked) {
       [self.brain pushOperand: [self.display.text doubleValue]]; 
       double result = [self.brain performOperation: self.brain.operation];
       // The line below this 
       self.display.text = [NSString stringWithFormat: @"%g", result];
    }
}

我正在使用斯坦福教授教授的计算器应用程序,他的课程发布在 iTunes U 中。但是,我对其进行了编辑,使其不包含 enterPressed 命令,而是使用了经过更改且对用户更友好的“equalsPressed”。equalsPressed 完成后(意思是当我单击计算器上的等号时),显示错误。可能有人知道是什么导致了这个问题吗?我已经发现其他人的错误与这个非常相似,但发生在稍微不同的地方。以下是可以帮助阐明问题的其他代码片段。

#import "CalculatorBrain.h"

@interface CalculatorBrain()
@property (nonatomic, strong) NSMutableArray *operandStack;
@end

@implementation CalculatorBrain

@synthesize operandStack = _operandStack;
@synthesize operandStackIsEmpty = _operandStackIsEmpty;
@synthesize operationIsPicked = _operationIsPicked;
@synthesize operation = _operation;

- (NSMutableArray *) operandStack {
    if (!_operandStack) {
        _operandStack =[[NSMutableArray alloc] init];
        _operandStackIsEmpty = YES;
        _operationIsPicked = NO;
    }
    return _operandStack;
}

- (void) resetStack {
    [self.operandStack removeAllObjects];
    self.operandStackIsEmpty = YES;
    self.operationIsPicked = NO;
}

- (void) pushOperand : (double) operand{
    [self.operandStack addObject: [NSNumber numberWithDouble:operand]];
    self.operandStackIsEmpty = NO;
}

- (double) popOperand {
    NSNumber *num = [self.operandStack lastObject];
    if (num) {[self.operandStack removeLastObject];}
    return [num doubleValue];
}

- (double) performOperation : (NSString *) operation{

    double result = 0;
    double num2 = [self popOperand];
    double num1 = [self popOperand];
    if ([operation isEqualToString:@"+"])
        result = num2 + num1;
    else if ([operation isEqualToString:@"-"])
        result = num2 - num1;
    else if ([operation isEqualToString:@"*"] || [operation isEqualToString:@"x"])
        result = num2 * num1;
    else if ([operation isEqualToString:@"/"]){
        if (num2 == 0)
            [self resetStack];
        else 
            result = num2 / num1;
    }

    [self pushOperand:result];
    self.operationIsPicked = NO;

    return result;

}

@end

此外,我在控制台中打印显示的调试 NSLog 工作正常,但计算器视图中的显示实际上并没有更新(当我使用断点来减慢功能时)。

#import "CalculatorViewController.h"
#import "CalculatorBrain.h"


@interface CalculatorViewController ()

@property (nonatomic) BOOL userIsInTheMiddleOfTypingANumber;
@property (nonatomic, strong) CalculatorBrain *brain;

@end


@implementation CalculatorViewController

@synthesize userIsInTheMiddleOfTypingANumber = _userIsInTheMiddleOfTypingANumber;
@synthesize brain = _brain;
@synthesize display = _display;

- (CalculatorBrain *) brain {

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

}

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

    NSString *digit = [sender currentTitle];
    if (self.userIsInTheMiddleOfTypingANumber)
        self.display.text = [self.display.text stringByAppendingString: digit];
    else {
        self.display.text = digit;
        self.userIsInTheMiddleOfTypingANumber = YES;
    }

}

- (IBAction) clearPressed {
    self.userIsInTheMiddleOfTypingANumber = NO;
    self.display.text = @"0";
    [self.brain resetStack];
}   


- (IBAction) equalsPressed {
    self.userIsInTheMiddleOfTypingANumber = NO;
    if (self.brain.operationIsPicked) {
        [self.brain pushOperand: [self.display.text doubleValue]]; 
        double result = [self.brain performOperation: self.brain.operation];
        self.display.text = [NSString stringWithFormat: @"%g", result];
        NSLog(@"%@",self.display.text);
    }
    NSLog(@"%@",self.display.text);
}

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

    if (self.userIsInTheMiddleOfTypingANumber) { 
        [self.brain pushOperand: [self.display.text doubleValue]]; 
        self.brain.operation = [sender currentTitle];
        self.brain.operationIsPicked = YES;
        self.userIsInTheMiddleOfTypingANumber = NO;
        NSLog(@"%@", sender.currentTitle);
    }
}

谢谢!

4

1 回答 1

1

引用评论中的答案:

“完整的错误说我向“[self.brain equalsPressed:]”发送了一个无效参数。实际上,equalsPressed 没有参数,但是当我将视图中的“=”按钮链接到其各自的方法时,我忘记了表示该函数不带参数。因此,即使我编写的函数没有参数,我也告诉编译器(开始)它应该带一个参数。

于 2014-10-09T14:15:45.857 回答