私はこの電卓アプリをxcode 4.5で構築しましたが、数字は期待どおりに機能しますが、操作、つまり (+,-,c) はNanとinfのみを返し、Enterボタンが常にアプリをクラッシュさせ、以下のエラーを返すため、一度も機能しませんでした.Iストーリーボードに UI を構築し、関連するメソッドにリンクされたボタン
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([CalculatorAppDelegate
class]));
これは私の CalculatorViewController.h です
#import <UIKit/UIKit.h>
@interface CalculatorViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *display;
@end
これは私の CalculatorViewController.m です
#import "CalculatorViewController.h"
#import "CalculatorBrain.h"
@interface CalculatorViewController ()
@property(nonatomic) BOOL userIsInTheMidleOfEnteringANumber;
@property (nonatomic, strong)CalculatorBrain *brain;
@end
@implementation CalculatorViewController
@synthesize display = _display;
@synthesize userIsInTheMidleOfEnteringANumber =_userIsInTheMidleOfEnteringANumber;
@synthesize brain = _brain;
- (CalculatorBrain *)brain
{
if (!_brain)_brain=[[CalculatorBrain alloc]init];
return _brain;
}
- (IBAction)digitPressed:(UIButton *)sender
{
NSString *digit = sender.currentTitle;
if (self.userIsInTheMidleOfEnteringANumber){
self.display.text = [self.display.text stringByAppendingString:digit];
}
else {
self.display.text = digit;
self.userIsInTheMidleOfEnteringANumber=YES;
}
}
- (IBAction)enterPressed
{
NSLog(@"im hre");
[self.brain pushOperand:[self.display.text doubleValue]];
self.userIsInTheMidleOfEnteringANumber=NO;
}
- (IBAction)operationPresed:(UIButton *)sender
{
if (self.userIsInTheMidleOfEnteringANumber) [self enterPressed];
double result =[self.brain performOperation:sender.currentTitle];
NSString *resultString= [NSString stringWithFormat:@"%g",result];
self.display.text= resultString;
}
@end
これは私の CalculatorBrain.h です
#import <Foundation/Foundation.h>
@interface CalculatorBrain : NSObject
-(void)pushOperand:(double)operand;
-(double)performOperation:(NSString *)operation;
@end
これは私の CalculatorBrain.m です
#import "CalculatorBrain.h"
@interface CalculatorBrain()
@property (nonatomic,strong)NSMutableArray *operandStack;
@end
@implementation CalculatorBrain
@synthesize operandStack = _operandStack;
-(NSMutableArray *)operandStack
{
if (_operandStack== nil) _operandStack =[[NSMutableArray alloc] init];
return _operandStack;
}
-(void)pushOperand:(double)operand
{
[self.operandStack addObject:[NSNumber numberWithDouble:operand]];
}
-(double)popOperand
{
NSNumber *operandObject = [self.operandStack lastObject];
if (operandObject) [self.operandStack removeLastObject];
return [operandObject doubleValue];
}
-(double)performOperation:(NSString *)operation
{
double result=0;
//calculate result
if ([operation isEqualToString:@"+"])
{
result =[self popOperand]+ [self popOperand];
}
else if ([operation isEqualToString:@"*"])
{
result = [self popOperand] * [self popOperand];
}
else if ([operation isEqualToString:@"-"]) {
result = [self popOperand] - [self popOperand];
}
else if ([operation isEqualToString:@"/"]) {
result = [self popOperand] / [self popOperand];
}
[self pushOperand:result];
return result;
}
@終わり
私は例に取り組んでおり、すべての手順に従っています。何が間違っているのか理解できません。enterPressed メソッドにはタイプ、つまり UIButton がありません。これは私のコンソール出力です
2013-05-17 15:05:15.221 Calculator[786:11303] im hre
2013-05-17 15:05:15.280 Calculator[786:11303] -[CalculatorViewController enterPressed:]: unrecognized selector sent to instance 0x922a620
2013-05-17 15:05:15.288 Calculator[786:11303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CalculatorViewController enterPressed:]: unrecognized selector sent to instance 0x922a620'
*** First throw call stack:
(0x1c8e012 0x10cbe7e 0x1d194bd 0x1c7dbbc 0x1c7d94e 0x10df705 0x16920 0x168b8 0xd7671 0xd7bcf 0xd6d38 0x4633f 0x46552 0x243aa 0x15cf8 0x1be9df9 0x1be9ad0 0x1c03bf5 0x1c03962 0x1c34bb6 0x1c33f44 0x1c33e1b 0x1be87e3 0x1be8668 0x1365c 0x1f0d 0x1e35)
libc++abi.dylib: terminate called throwing an exception
(lldb)
どんな助けでも大歓迎です