gpt4 book ai didi

ios - Objective-C 问题 : EXC_BAD_ACCESS code=2

转载 作者:行者123 更新时间:2023-11-29 04:06:44 25 4
gpt4 key购买 nike

我正在尝试编写一个计算器程序,但是当按下链接到函数表达式评估的特定键时,我遇到了此错误。我的计算器工作正常,但我正在尝试添加使用/存储变量的能力。为此,我使用表达式评估按钮。但是,一旦我按下链接到该方法的按钮,整个应用程序就会崩溃,并且 xcode 会给出错误 EXC_BAD_ACCESS code=2。我是 Objective-C 的新手,所以我不知道如何调试这个错误。但据我所知,问题在于该行

double result = [CalculatorBrain evaluateExpression:self.brain.expression usingVariableValues:testValues];

...在我的 View Controller 中。我在这条线之前设置了一个断点,它没有崩溃,但是在这条线设置它导致了我遇到的崩溃。我尝试削减下面的代码,并且仅包含直接链接到表达式评估按钮的内容。感谢您的帮助!

这是我的大脑方法的 .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

这是我的 View Controller 的 .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];

感谢所有提供帮助的人!

关于ios - Objective-C 问题 : EXC_BAD_ACCESS code=2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15078448/

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