0

私はobjective-cにまったく慣れていないので、なぜこれが起こっているのかわかりません。私はまだ概念に頭を悩ませようとしていて、何を探しているのかわからないので、親切で賢い人が私を助けてくれることを願っています. きっと、自分が問題だと気付いていないのは馬鹿げたことだと思います。

ViewController.h:

#import <UIKit/UIKit.h>
#import "CalculatorBrain.h"

@interface ViewController : UIViewController {
    IBOutlet UILabel *display;
    CalculatorBrain *brain;
    BOOL userIsInTheMiddleOfTypingANumber;
}

- (IBAction)digitPressed:(UIButton *)sender;
- (IBAction)operationPressed:(UIButton *)sender;

@end

ViewController.m: ("NSString *operation = [[sender titleLabel] text];" でクラッシュが発生している)

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (CalculatorBrain *)brain
{
    if (brain) {
        brain = [[CalculatorBrain alloc] init];
    }
    return brain;
}

- (IBAction)digitPressed:(UIButton *)sender
{
    NSString *digit = [[sender titleLabel] text];
    if (userIsInTheMiddleOfTypingANumber) {
        [display setText:[[display text] stringByAppendingString:digit]];
    } else {
        [display setText:digit];
        userIsInTheMiddleOfTypingANumber = YES;
    }
}

- (IBAction)operationPressed:(UIButton *)sender
{
    if (userIsInTheMiddleOfTypingANumber) {
        [[self brain] setOperand:[[display text] doubleValue]];
        userIsInTheMiddleOfTypingANumber = NO;
    }
    NSString *operation = [[sender titleLabel] text];
    double result = [[self brain] performOperation:operation];
    [display setText:[NSString stringWithFormat:@"%g", result]];
}

@end

CalculatorBrain.h:

#import <Foundation/Foundation.h>

@interface CalculatorBrain : NSObject {
    double operand;
    NSString *waitingOperation;
    double waitingOperand;
}

- (void)setOperand:(double)anOperand;
- (double)performOperation:(NSString *)operation;

@end

CalculatorBrain.m:

#import "CalculatorBrain.h"

@implementation CalculatorBrain

- (void)setOperand:(double)anOperand
{
    operand = anOperand;
}

- (void)performWaitingOperation
{
    if ([@"+" isEqual:waitingOperation]) {
        operand = waitingOperand + operand;
    } else if ([@"-" isEqual:waitingOperation]) {
        operand = waitingOperand - operand;
    } else if ([@"*" isEqual:waitingOperation]) {
        operand = waitingOperand * operand;
    } else if ([@"/" isEqual:waitingOperation]) {
        if (operand) {
        operand = waitingOperand / operand;
        }
    }
}

- (double)performOperation:(NSString *)operation
{
    if ([operation isEqual:@"sqrt"]) {
        operand = sqrt(operand);
    } else {
        [self performWaitingOperation];
        waitingOperation = operation;
        waitingOperand = operand;
    }
    return operand;
}

@end

助けやヒントを事前にありがとう...私は自分が何をしているのか分かりません:)

4

3 に答える 3

1
- (CalculatorBrain *)brain {
 if (brain) {
     brain = [[CalculatorBrain alloc] init];
 }     
 return brain; 
 } 

コードには他にも問題がある可能性がありますが、これはすぐに突き出しました。! がありません。オペレーター。

- (CalculatorBrain *)brain {
 if (!brain) {
     brain = [[CalculatorBrain alloc] init];
 }     
 return brain; 
 } 
于 2012-07-29T09:24:44.663 に答える
0

XCode のデバッグ コンソールでクラッシュの原因を見つけることができるはずです。

補足として、[sender currentTitle]代わりに? を使用しないのはなぜ[[titleLabel] text]ですか?

于 2012-07-29T08:08:20.993 に答える
0

これが古いことは知っていますが、この質問者とまったく同じ問題があり、同じ行に誤ってブレークポイントを追加したことがわかりました。これがこの問題を抱えている他の人に役立つことを願っています。

于 2013-02-07T16:33:30.787 に答える