[英]Objective-C Trouble: EXC_BAD_ACCESS code=2
我正在嘗試編寫一個計算器程序,但是在按下鏈接到函數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
這是.m:
// CalculatorBrain.m
#import "CalculatorBrain.h"
#define VARIABLE_PREFIX @"^"
@interface CalculatorBrain()
@property (nonatomic) double operand;
@property (nonatomic, strong) NSString *waitingOperation;
@property (nonatomic) double waitingOperand;
@property (nonatomic) double storedOperand;
@property (nonatomic) NSMutableArray *internalExpression;
@end
@implementation CalculatorBrain
@synthesize operand = _operand; //operand in use
@synthesize waitingOperation = _waitingOperation; //waiting to be computed
@synthesize waitingOperand = _waitingOperand; //waiting to be used
@synthesize storedOperand = _storedOperand; //stored in memory
@synthesize internalExpression = _internalExpression; //private expression stored
@synthesize expression = _expression;
- (id) expression {;
return [NSMutableArray arrayWithArray:self.internalExpression];
}
//here I have instance methods that add objects to the array internalExpression
//these work fine as far as I can tell
+ (double) evaluateExpression:(id)anExpression usingVariableValues:(NSDictionary *)variables {
double result = 0;
int count = [anExpression count];
CalculatorBrain *brain;
for (int i = 0; i < count; i++) {
if([[anExpression objectAtIndex:i] isKindOfClass:[NSNumber class]]) {
double operand = [[anExpression objectAtIndex:i] doubleValue];
[brain pushOperand:operand];
}
if([[anExpression objectAtIndex:i] isKindOfClass:[NSString class]]) {
NSString *string = [anExpression objectAtIndex:i];
if([string characterAtIndex:0] == '^') {
NSString* variable = [NSString stringWithFormat:@"%c",[string characterAtIndex:1]];
double valueOfVariable = [[variables objectForKey:variable] doubleValue];
[brain pushOperand:valueOfVariable];
}
else {
NSString* operator = [anExpression objectAtIndex:i];
result = [brain performOperation:operator];
}
}
}
return result;
}
@end
這是我的視圖控制器的.h:
// CalcViewController.h
#import <UIKit/UIKit.h>
//view controller for calculator
@interface CalcViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *display; //real-time display
@property (strong, nonatomic) IBOutlet UILabel *miniDisplay; //past display
@property (strong, nonatomic) IBOutlet UILabel *memDisplay; //display stored digits
- (IBAction)digitPressed:(UIButton *)sender; //pressed number
- (IBAction)operandPressed:(UIButton *)sender; //pressed operation
- (IBAction)variablePressed:(UIButton *)sender; //pressed variable
- (IBAction)expressionEvaluation:(UIButton *)sender; //evaluate expression with variables
@end
這是.m:
// 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];
感謝所有提供幫助的人!
通過在xCode中添加異常斷點,您將獲得更多有關引發異常的代碼行的信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.