我刚开始使用 iOS 开发,并认为一个好的第一个应用程序应该是一个简单的计算器。不幸的是,每当我尝试运行该应用程序时,它都会因错误“此类不符合键 numberFour 的键值编码”而崩溃。
这是 ViewController.h:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
- (IBAction)numberZero:(id)sender;
- (IBAction)numberOne:(id)sender;
- (IBAction)numberTwo:(id)sender;
- (IBAction)numberThree:(id)sender;
- (IBAction)numberFour:(id)sender;
- (IBAction)numberFive:(id)sender;
- (IBAction)numberSix:(id)sender;
- (IBAction)numberSeven:(id)sender;
- (IBAction)numberNine:(id)sender;
- (IBAction)numberEight:(id)sender;
@property (weak, nonatomic) IBOutlet UILabel *expressionView;
-(void)updateExpressionView;
@property NSNumber *operandOne;
@property NSNumber *operandTwo;
@property NSNumber *one;
@property NSNumber *two;
@property NSNumber *three;
@property NSNumber *four;
@property NSNumber *five;
@property NSNumber *six;
@property NSNumber *seven;
@property NSNumber *eight;
@property NSNumber *nine;
@property NSNumber *zero;
@property NSMutableArray *expression;
@end
这是 ViewController.m:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.one = [NSNumber numberWithInt: 1];
self.two = [NSNumber numberWithInt:2];
self.three = [NSNumber numberWithInt:3];
self.four = [NSNumber numberWithInt: 4];
self.five = [NSNumber numberWithInt:5];
self.six = [NSNumber numberWithInt: 6];
self.seven = [NSNumber numberWithInt:7];
self.eight = [NSNumber numberWithInt:8];
self.nine = [NSNumber numberWithInt: 9];
self.zero = [NSNumber numberWithInt:0];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)numberOne:(id)sender {
[self.expression addObject:self.one];
[self updateExpressionView];
}
- (IBAction)numberTwo:(id)sender {
[self.expression addObject: self.two];
[self updateExpressionView];
}
- (IBAction)numberThree:(id)sender {
[self.expression addObject: self.three];
[self updateExpressionView];
}
- (IBAction)numberFour:(id)sender {
[self.expression addObject: self.four];
[self updateExpressionView];
}
- (IBAction)numberFive:(id)sender {
[self.expression addObject: self.five];
[self updateExpressionView];
}
- (IBAction)numberSix:(id)sender {
[self.expression addObject:self.six];
[self updateExpressionView];
}
- (IBAction)numberSeven:(id)sender {
[self.expression addObject: self.seven];
[self updateExpressionView];
}
- (IBAction)numberNine:(id)sender {
[self.expression addObject: self.nine];
[self updateExpressionView];
}
- (IBAction)numberZero:(id)sender {
[self.expression addObject: self.zero];
[self updateExpressionView];
}
- (IBAction)numberEight:(id)sender {
[self.expression addObject: self.eight];
[self updateExpressionView];
}
-(void)updateExpressionView {
NSString *expressionNSString = (NSString *)self.expression;
self.expressionView.text = expressionNSString;
}
@end