Ios 目标C故障:EXC\u错误\u访问代码=2

Ios 目标C故障:EXC\u错误\u访问代码=2,ios,objective-c,memory-leaks,calculator,Ios,Objective C,Memory Leaks,Calculator,我正试图编写一个计算器程序,但当按下与函数expressionEvaluation链接的特定键时,我遇到了这个错误。我的计算器工作正常,但我正在尝试添加使用/存储变量的功能。为此,我使用expressionEvaluation按钮。然而,只要我按下链接到该方法的按钮,整个应用程序就会崩溃,xcode就会给出错误EXC_BAD_ACCESS code=2。我是objective-c新手,因此不确定如何调试此错误。不过,据我所知,问题在于线路 double result = [CalculatorB

我正试图编写一个计算器程序,但当按下与函数expressionEvaluation链接的特定键时,我遇到了这个错误。我的计算器工作正常,但我正在尝试添加使用/存储变量的功能。为此,我使用expressionEvaluation按钮。然而,只要我按下链接到该方法的按钮,整个应用程序就会崩溃,xcode就会给出错误EXC_BAD_ACCESS code=2。我是objective-c新手,因此不确定如何调试此错误。不过,据我所知,问题在于线路

double result = [CalculatorBrain evaluateExpression:self.brain.expression usingVariableValues:testValues];
…在我的视图控制器中。我在这条线之前设置了一个断点,它没有崩溃,但是在这条线上设置它导致了我正在经历的崩溃。 我试图将下面的代码删减,只包含了与expressionEvaluation按钮直接链接的内容。谢谢你的帮助

以下是我的大脑方法的.h:

//  CalculatorBrain.h

#import <Foundation/Foundation.h>

@interface CalculatorBrain : NSObject
- (void) pushOperand:(double)operand;
- (void) setVariableAsOperand:(NSString *)variableName;
- (void) performWaitingOperation;
- (double) performOperation:(NSString *)operation;

@property (readonly) id expression;
+ (double)evaluateExpression: (id)anExpression
         usingVariableValues: (NSDictionary *)variables;
+ (NSSet *)variablesInExpression:(id)anExpression;
+ (NSString *)descriptionOfExpression:(id)anExpression;

+ (id)propertyListForExpression:(id)anExpression;
+ (id)expressionForPropertyList:(id)propertyList;

@end

编辑:作为旁注,我没有收到任何错误,只有一个警告:未完成实现。这是因为我还没有完成几个类方法,但我认为这不会导致崩溃。

通过在xCode中添加异常断点,您将获得有关引发异常的代码行的更多信息

NSDictionary* testValues = [NSDictionary dictionaryWithObjectsAndKeys:@"x", 2, @"y", 3, @"z", 4, nil];
原来是错误的路线。应该是:

NSDictionary* testValues = [NSDictionary dictionaryWithObjectsAndKeys:@"x", [NSNumber numberWithInt:2], @"y", [NSNumber numberWithInt:3], @"z", [NSNumber numberWithInt:4], nil];

感谢所有帮助过我的人

打开异常断点:@lottscarson我按照步骤添加了一个异常断点,然后尝试运行它,但它在同一位置崩溃,之前没有引发任何异常。不过,给出异常的行是:0x10e5098:movl(%eax),%edx当您查看左侧的调用堆栈时,如果你往下走,你应该能够在某一点上找到你自己的代码(它应该是黑色文本而不是灰色文本)。
//  CalcViewController.m

#import "CalcViewController.h"
#import "CalculatorBrain.h"

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

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

- (IBAction)expressionEvaluation:(UIButton *)sender {
    NSDictionary* testValues = [NSDictionary dictionaryWithObjectsAndKeys:@"x", 2, @"y", 3, @"z", 4, nil];
    double result = [CalculatorBrain evaluateExpression:self.brain.expression usingVariableValues:testValues];
    NSString *resultString = [NSString stringWithFormat:@"%g", result];
    self.display.text = resultString;
}


@end
NSDictionary* testValues = [NSDictionary dictionaryWithObjectsAndKeys:@"x", 2, @"y", 3, @"z", 4, nil];
NSDictionary* testValues = [NSDictionary dictionaryWithObjectsAndKeys:@"x", [NSNumber numberWithInt:2], @"y", [NSNumber numberWithInt:3], @"z", [NSNumber numberWithInt:4], nil];