未被捕获的异常' NSUnknownKeyException' - 无法找到导致问题的变量

时间:2018-02-27 22:16:50

标签: ios objective-c xcode xcode9

我知道在这个网站上有类似的问题处理这个错误,但我无法弄清楚我的代码中导致错误的不协调。

完整错误是:

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: 
'[<ViewController 0x7ffe26d05a50> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key description.'

我不知道或在我的代码中看到任何我称之为&#34;描述的内容。&#34;

到目前为止,这是我的代码:

的main.m

#import <UIKit/UIKit.h>
#import "AppDelegate.h"

int main(int argc, char * argv[]) {
@autoreleasepool {
    return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
  }
}

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()
@property NSMutableArray *numbers;
@property (weak, nonatomic) IBOutlet UIImageView *image;
@property (weak, nonatomic) IBOutlet UITextField *nameField;
@property (weak, nonatomic) IBOutlet UITextField *descriptionField;
@property (weak, nonatomic) IBOutlet UITextField *costField;
@property (weak, nonatomic) IBOutlet UIPickerView *numSlider;

@end

@implementation ViewController

- (IBAction)pressedBackward:(id)sender {
    self.nameField.text = @"backward";
    self.descriptionField.text = @"backward";
    self.costField.text = @"backward";
}

- (IBAction)pressedForward:(id)sender {
    self.nameField.text = @"forward";
    self.descriptionField.text = @"forward";
    self.costField.text = @"forward";
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{return 1;}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{return _numbers.count;}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return [[NSString alloc] initWithFormat:@"%@", _numbers[row]];
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
}

- (void)viewDidLoad {
    [super viewDidLoad];
    for (int i = 0; i < 100; i++) {
        [_numbers addObject:[NSNumber numberWithInt:i]];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

我非常感谢您理解为什么我收到此错误并且无法运行我的程序。

1 个答案:

答案 0 :(得分:0)

我认为问题在于使用 .xib 文件错误映射了一个UI元素。

请检查元素image, nameField, descriptionField, costField, numSlider是否正确链接,或者xib / storyboard文件中是否有一个元素在ViewController中链接但已被删除。