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