我刚刚开始使用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
答案 0 :(得分:1)
在xib或storyboard中,此场景没有正确指定View Controller的类型。当您为计算器选择场景时,可以在界面构建器中找到它,并检查其属性(请参见附图)。您需要将该类设置为从UIViewController继承的自定义类的名称(在本例中为ViewController)。
答案 1 :(得分:0)
您的.xib或storyboard文件中没有连接numberFour,或者文件所有者更可能不是ViewController类型。
答案 2 :(得分:0)
很抱歉没有将此作为评论添加,我没有足够的声誉(最低要求50分)。
之前已经问过这个问题,请看SO:What does this mean? "'NSUnknownKeyException', reason: … this class is not key value coding-compliant for the key X"就是一个例子。
ViewController
?答案 3 :(得分:0)
您确定所有IBAction都是xib上的链接或故事板中的视图吗?
在添加对象之前,您还需要初始化数组:
self.expression = [[NSMutableArray alloc] init]; // add this line just after [super viewDidLoad];