-1

我刚开始使用 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
4

4 回答 4

1

在您的 xib 或情节提要中,此场景没有正确指定视图控制器的类型。当您为计算器选择场景并检查其属性时,您可以在界面构建器中找到它(参见附图)。您需要将该类设置为继承自 UIViewController 的自定义类的名称(在本例中为 ViewController)。

查看控制器属性

于 2013-10-03T19:14:05.820 回答
0

很抱歉没有将此添加为评论,我没有足够的声誉来这样做(至少需要 50 个)。

以前有人问过这个问题,请看SO:这是什么意思?“'NSUnknownKeyException', reason: ... 这个类不符合键 X 的键值编码”就是一个例子。

  1. 您是否检查过您的视图(在您的故事板中)是否使用了正确的控制器ViewController
  2. 您是否将 Storyboard 中的 IBAction 与实际视图正确连接?(如果 nr.1 是正确的,根据我的经验,这通常并不重要)。
于 2013-10-03T19:09:02.747 回答
0

您的 .xib 或故事板文件中没有连接 numberFour,或者文件所有者很可能不是 ViewController 类型。

于 2013-10-03T19:07:32.510 回答
0

您确定您所有的 IBAction 都是您的 xib 上的链接或情节提要中的视图吗?

您还需要在添加对象之前初始化您的数组:

self.expression = [[NSMutableArray alloc] init]; // add this line just after [super viewDidLoad];
于 2013-10-03T19:10:23.873 回答