0

電卓プログラムを作成しようとしていますが、関数 expressionEvaluation にリンクされている特定のキーを押すと、このエラーが発生します。私の電卓は正常に動作しますが、変数を使用/保存する機能を追加しようとしています。このために、expressionEvaluation ボタンを使用します。ただし、そのメソッドにリンクされているボタンを押すとすぐに、アプリ全体がクラッシュし、xcode でエラー EXC_BAD_ACCESS code=2 が表示されます。Objective-C は初めてなので、このエラーをデバッグする方法がわかりません。ただし、私が知る限り、問題は行にあります

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

...私のView Controllerで。この行の前にブレーク ポイントを設定しましたが、クラッシュしませんでしたが、この行に設定するとクラッシュが発生しました。以下のコードを切り詰めようとして、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

私の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

編集: 補足として、エラーは発生しません。警告は 1 つだけです: 不完全な実装です。これは、いくつかのクラス メソッドをまだ完成させていないためですが、それがクラッシュの原因になっているとは思いません。

4

2 に答える 2

1
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];

助けてくれたみんなに感謝します!

于 2013-02-26T00:38:25.337 に答える
0

xCode に例外ブレークポイントを追加することで、例外を発生させるコード行に関する詳細情報を取得できます。

于 2013-02-25T23:33:05.947 に答える