纠结好久不知道哪里错了!!请各位大神看看!!多谢~~~
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;
-(double)popOperand;
@end
@implementation CalculatorBrain
@synthesize operandStack = _operandStack;
-(void)pushOperand:(double)operand{
NSNumber *operandObject = [NSNumber numberWithDouble:operand];
[self.operandStack addObject:operandObject];
}
-(double)popOperand{
NSNumber *operandObject = [self.operandStack lastObject];
if(operandObject != nil) [self.operandStack removeLastObject];
return [operandObject doubleValue];
}-(double)performOperation:(NSString *)operation{
double result = 0;
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;
}
-(void)setOperandStack:(NSMutableArray *)operandStack{
_operandStack = operandStack;
}
-(NSMutableArray *)operandStack{
if(_operandStack == nil){
_operandStack = [[NSMutableArray alloc] init];
}
return _operandStack;
}
@end
XYZViewController.h
#import <UIKit/UIKit.h>
#import "CalculatorBrain.h"
@interface XYZViewController : UIViewController
@property (nonatomic) BOOL userIsInTheMiddleOfEnteringANumber;
@property (strong, nonatomic) IBOutlet UILabel *display;
@property (nonatomic,strong) CalculatorBrain *brain;
- (IBAction)digitPressed:(UIButton *)sender;
- (IBAction)operatorPressed:(UIButton *)sender;
- (IBAction)enterPressed;
@endXYZViewController.m
#import "XYZViewController.h"
#import "CalculatorBrain.h"
@implementation XYZViewController
@synthesize display = _display;
@synthesize userIsInTheMiddleOfEnteringANumber = _userIsInTheMiddleOfEnteringANumber;
@synthesize brain = _brian;
-(CalculatorBrain *)brain{
if(!_brian){
_brian = [[CalculatorBrain alloc] init];
}
return _brian;
}
- (IBAction)digitPressed:(UIButton *)sender {
NSString *num = [sender currentTitle];
if(self.userIsInTheMiddleOfEnteringANumber){
UILabel *myDisplay = self.display;
NSString *currentText = myDisplay.text;
NSString *newText = [currentText stringByAppendingString:num];
[myDisplay setText:newText];
}else{
UILabel *myDisplay = self.display;
[myDisplay setText:num];
self.userIsInTheMiddleOfEnteringANumber = YES;
}
}
- (IBAction)operatorPressed:(UIButton *)sender {
if(self.userIsInTheMiddleOfEnteringANumber) [self enterPressed];
double result = [self.brain performOperation:[sender currentTitle]];
self.display.text = [NSString stringWithFormat:@"%g",result];
}
- (IBAction)enterPressed{
[self.brain pushOperand:[self.display.text doubleValue]];
self.userIsInTheMiddleOfEnteringANumber = NO;
}
@end
控制台输出:
2014-08-26 21:21:44.070 Calculator[3733:60b] -[XYZViewController enterPressed:]: unrecognized selector sent to instance 0x8f56ce0
2014-08-26 21:21:44.073 Calculator[3733:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[XYZViewController enterPressed:]: unrecognized selector sent to instance 0x8f56ce0'
*** First throw call stack:
(
0 CoreFoundation 0x017ed1e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x0156c8e5 objc_exception_throw + 44
2 CoreFoundation 0x0188a243 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
3 CoreFoundation 0x017dd50b ___forwarding___ + 1019
4 CoreFoundation 0x017dd0ee _CF_forwarding_prep_0 + 14
5 libobjc.A.dylib 0x0157e880 -[NSObject performSelector:withObject:withObject:] + 77
6 UIKit 0x0022e3b9 -[UIApplication sendAction:to:from:forEvent:] + 108
7 UIKit 0x0022e345 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
8 UIKit 0x0032fbd1 -[UIControl sendAction:to:forEvent:] + 66
9 UIKit 0x0032ffc6 -[UIControl _sendActionsForEvents:withEvent:] + 577
10 UIKit 0x0032f243 -[UIControl touchesEnded:withEvent:] + 641
11 UIKit 0x0026dddd -[UIWindow _sendTouchesForEvent:] + 852
12 UIKit 0x0026e9d1 -[UIWindow sendEvent:] + 1117
13 UIKit 0x002405f2 -[UIApplication sendEvent:] + 242
14 UIKit 0x0022a353 _UIApplicationHandleEventQueue + 11455
15 CoreFoundation 0x0177677f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
16 CoreFoundation 0x0177610b __CFRunLoopDoSources0 + 235
17 CoreFoundation 0x017931ae __CFRunLoopRun + 910
18 CoreFoundation 0x017929d3 CFRunLoopRunSpecific + 467
19 CoreFoundation 0x017927eb CFRunLoopRunInMode + 123
20 GraphicsServices 0x037e15ee GSEventRunModal + 192
21 GraphicsServices 0x037e142b GSEventRun + 104
22 UIKit 0x0022cf9b UIApplicationMain + 1225
23 Calculator 0x00002c8d main + 141
24 libdyld.dylib 0x01e34701 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
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;
-(double)popOperand;
@end
@implementation CalculatorBrain
@synthesize operandStack = _operandStack;
-(void)pushOperand:(double)operand{
NSNumber *operandObject = [NSNumber numberWithDouble:operand];
[self.operandStack addObject:operandObject];
}
-(double)popOperand{
NSNumber *operandObject = [self.operandStack lastObject];
if(operandObject != nil) [self.operandStack removeLastObject];
return [operandObject doubleValue];
}-(double)performOperation:(NSString *)operation{
double result = 0;
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;
}
-(void)setOperandStack:(NSMutableArray *)operandStack{
_operandStack = operandStack;
}
-(NSMutableArray *)operandStack{
if(_operandStack == nil){
_operandStack = [[NSMutableArray alloc] init];
}
return _operandStack;
}
@end
XYZViewController.h
#import <UIKit/UIKit.h>
#import "CalculatorBrain.h"
@interface XYZViewController : UIViewController
@property (nonatomic) BOOL userIsInTheMiddleOfEnteringANumber;
@property (strong, nonatomic) IBOutlet UILabel *display;
@property (nonatomic,strong) CalculatorBrain *brain;
- (IBAction)digitPressed:(UIButton *)sender;
- (IBAction)operatorPressed:(UIButton *)sender;
- (IBAction)enterPressed;
@endXYZViewController.m
#import "XYZViewController.h"
#import "CalculatorBrain.h"
@implementation XYZViewController
@synthesize display = _display;
@synthesize userIsInTheMiddleOfEnteringANumber = _userIsInTheMiddleOfEnteringANumber;
@synthesize brain = _brian;
-(CalculatorBrain *)brain{
if(!_brian){
_brian = [[CalculatorBrain alloc] init];
}
return _brian;
}
- (IBAction)digitPressed:(UIButton *)sender {
NSString *num = [sender currentTitle];
if(self.userIsInTheMiddleOfEnteringANumber){
UILabel *myDisplay = self.display;
NSString *currentText = myDisplay.text;
NSString *newText = [currentText stringByAppendingString:num];
[myDisplay setText:newText];
}else{
UILabel *myDisplay = self.display;
[myDisplay setText:num];
self.userIsInTheMiddleOfEnteringANumber = YES;
}
}
- (IBAction)operatorPressed:(UIButton *)sender {
if(self.userIsInTheMiddleOfEnteringANumber) [self enterPressed];
double result = [self.brain performOperation:[sender currentTitle]];
self.display.text = [NSString stringWithFormat:@"%g",result];
}
- (IBAction)enterPressed{
[self.brain pushOperand:[self.display.text doubleValue]];
self.userIsInTheMiddleOfEnteringANumber = NO;
}
@end
控制台输出:
2014-08-26 21:21:44.070 Calculator[3733:60b] -[XYZViewController enterPressed:]: unrecognized selector sent to instance 0x8f56ce0
2014-08-26 21:21:44.073 Calculator[3733:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[XYZViewController enterPressed:]: unrecognized selector sent to instance 0x8f56ce0'
*** First throw call stack:
(
0 CoreFoundation 0x017ed1e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x0156c8e5 objc_exception_throw + 44
2 CoreFoundation 0x0188a243 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
3 CoreFoundation 0x017dd50b ___forwarding___ + 1019
4 CoreFoundation 0x017dd0ee _CF_forwarding_prep_0 + 14
5 libobjc.A.dylib 0x0157e880 -[NSObject performSelector:withObject:withObject:] + 77
6 UIKit 0x0022e3b9 -[UIApplication sendAction:to:from:forEvent:] + 108
7 UIKit 0x0022e345 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
8 UIKit 0x0032fbd1 -[UIControl sendAction:to:forEvent:] + 66
9 UIKit 0x0032ffc6 -[UIControl _sendActionsForEvents:withEvent:] + 577
10 UIKit 0x0032f243 -[UIControl touchesEnded:withEvent:] + 641
11 UIKit 0x0026dddd -[UIWindow _sendTouchesForEvent:] + 852
12 UIKit 0x0026e9d1 -[UIWindow sendEvent:] + 1117
13 UIKit 0x002405f2 -[UIApplication sendEvent:] + 242
14 UIKit 0x0022a353 _UIApplicationHandleEventQueue + 11455
15 CoreFoundation 0x0177677f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
16 CoreFoundation 0x0177610b __CFRunLoopDoSources0 + 235
17 CoreFoundation 0x017931ae __CFRunLoopRun + 910
18 CoreFoundation 0x017929d3 CFRunLoopRunSpecific + 467
19 CoreFoundation 0x017927eb CFRunLoopRunInMode + 123
20 GraphicsServices 0x037e15ee GSEventRunModal + 192
21 GraphicsServices 0x037e142b GSEventRun + 104
22 UIKit 0x0022cf9b UIApplicationMain + 1225
23 Calculator 0x00002c8d main + 141
24 libdyld.dylib 0x01e34701 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
[XYZViewController enterPressed:]
发送的是带参数的enterPressed:消息
而你在controller中定义的IBAction 如下
- (IBAction)enterPressed; ///////无参
所以调用时因找不到带参数的enterPressed方法而报错修改controller的enterPressed的声明及实现
////////头文件 .h
- (IBAction)enterPressed:(id)sender;
//////实现文件 .m
- (IBAction)enterPressed:(id)sender {
//////////......to do your logic
}