this class is not key value coding-compliant for the key error

一曲冷凌霜 提交于 2020-01-06 20:34:26

问题


My problem is when I build the application it has this error

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

Here is my header file

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

@interface FirstViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *timeOutOfBraceLabel;
@property (weak, nonatomic) IBOutlet UILabel *weeklyGoalLabel;
@property (strong, nonatomic) Brace *brace;

- (IBAction)minuteTimeOutOfBraceChanged:(id)sender;
//- (IBAction)hourTimeOutOfBraceChanged:(id)sender;

- (void)updateUI;
- (void)retreiveData;
- (void)checkTime;

@end

Here is my m file:

#import "FirstViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

- (Brace *)brace {
if (!_brace) {
    _brace = [[Brace alloc] init];
}
return _brace;
}

- (void)viewDidLoad
{
[self retreiveData];
[self updateUI];
[super viewDidLoad];
}

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

- (void)updateUI {
// Update timeOutOfBrace
NSString *timeOutOfBraceString = [[NSString alloc] init];
NSString *timeOutOfBraceHoursString = [[NSString alloc] init];
NSString *timeOutOfBraceMinutesString = [[NSString alloc] init];
NSLog(@"%d", self.brace.timeOutOfBrace.todayHours);
timeOutOfBraceHoursString = [NSString stringWithFormat:@"%d", self.brace.timeOutOfBrace.todayHours];
if (self.brace.timeOutOfBrace.todayMinutes < 10) {
    timeOutOfBraceMinutesString = [NSString stringWithFormat:@"0%d", self.brace.timeOutOfBrace.todayMinutes];
}
else {
    timeOutOfBraceMinutesString = [NSString stringWithFormat:@"%d", self.brace.timeOutOfBrace.todayMinutes];
}
timeOutOfBraceString = [NSString stringWithFormat:@"%@:%@", timeOutOfBraceHoursString, timeOutOfBraceMinutesString];
self.timeOutOfBraceLabel.text = timeOutOfBraceString;

// Update weeklyGoal
NSString *weeklyGoalString = [[NSString alloc] init];
NSString *totalHoursThisWeekString = [[NSString alloc] init];
int totalHoursThisWeekDecimal = self.brace.timeOutOfBrace.overallHours + round(self.brace.timeOutOfBrace.overallMinutes/60);
totalHoursThisWeekString = [NSString stringWithFormat:@"%d", totalHoursThisWeekDecimal];
weeklyGoalString = [NSString stringWithFormat:@"%d", self.brace.timeOutOfBrace.goalForWeek];
self.weeklyGoalLabel.text = [NSString stringWithFormat:@"%@/%@", totalHoursThisWeekString, weeklyGoalString];
}

- (void)retreiveData {
self.brace.timeOutOfBrace.todayHours = 4;
self.brace.timeOutOfBrace.todayMinutes = 0;
self.brace.timeOutOfBrace.overallHours = 12;
self.brace.timeOutOfBrace.overallMinutes = 0;
self.brace.timeOutOfBrace.goalForWeek = 28;
}

- (IBAction)minuteTimeOutOfBraceChanged:(id)sender {
if ([[sender currentTitle] isEqualToString:@"+"]) {
    self.brace.timeOutOfBrace.todayMinutes = [self.brace timeOutOfBraceChanged:YES
                                                                              :@"minutes"
                                                                              :self.brace.timeOutOfBrace.todayMinutes];
}
else {
    self.brace.timeOutOfBrace.todayMinutes = [self.brace timeOutOfBraceChanged:NO
                                                                              :@"minutes"
                                                                              :self.brace.timeOutOfBrace.todayMinutes];
}
[self checkTime];
}

- (IBAction)hourTimeOutOfBraceChanged:(id)sender {
if ([[sender currentTitle] isEqualToString:@"+"]) {
    self.brace.timeOutOfBrace.todayMinutes = [self.brace timeOutOfBraceChanged:YES
                                                                              :@"hours"
                                                                              :self.brace.timeOutOfBrace.todayHours];
}
else {
    self.brace.timeOutOfBrace.todayMinutes = [self.brace timeOutOfBraceChanged:NO
                                                                              :@"hours"
                                                                              :self.brace.timeOutOfBrace.todayHours];
}
[self checkTime];
} 

- (void)checkTime {
if (self.brace.timeOutOfBrace.todayMinutes < 0) {
    self.brace.timeOutOfBrace.todayMinutes = 45;
    self.brace.timeOutOfBrace.todayHours -= 1;
    }
    if (self.brace.timeOutOfBrace.todayMinutes > 45) {
        self.brace.timeOutOfBrace.todayMinutes = 0;
        self.brace.timeOutOfBrace.todayHours += 1;
    }
}

@end

I honestly have tried every possible solution that I know. I checked all the connecctions to my view and they were fine. Thank you in advance.


回答1:


If you have provided a key in interface builder to some UILabel instance of that missing key and then removed,that causes that error. Ctrl click the file's owner of view controller and remove the missing key from there.




回答2:


It look like you are creating an FirstViewController object with a nib file. You're probably doing something like:

[[[FirstViewController alloc] initWithNibName:@"AnOtherXibFile" bundle:nil] autorelease];

It mean it will create your FirstViewController object liked to the view compile into your AnOtherXibFile.xib file.

Can you double check you create FirstViewController with it correct xib file ?
You should have something like:

[[[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil] autorelease];

Or if you use the correct xib, double check all the reference to your ViewController...



来源:https://stackoverflow.com/questions/17475750/this-class-is-not-key-value-coding-compliant-for-the-key-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!