斯坦福iphone开发计算器示例在选择操作时崩溃



我对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*操作=[[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

提前感谢您的帮助或提示。。。我不知道自己在做什么:)

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

尽管代码可能还有其他问题,但这一点很快就突出了。你错过了!操作人员

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

您应该能够在XCode的调试控制台中找到崩溃的原因。

顺便说一下,为什么不使用[sender currentTitle]而不是[[titleLabel] text]呢?

我知道这是旧的,但我遇到了与这个询问者完全相同的问题,我发现我不小心在同一行添加了一个断点。希望这能帮助其他人解决这个问题。

最新更新