objective-c - 此类与键错误的键值编码不兼容

标签 objective-c error-handling runtime nserror

我的问题是当我构建应用程序时出现此错误

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

这是我的头文件
#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

这是我的 m 文件:
#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

老实说,我已经尝试了我所知道的所有可能的解决方案。我检查了与我的 View 的所有连接,它们都很好。先感谢您。

最佳答案

如果您在接口(interface)生成器中为该缺失键的某些 UILabel 实例提供了一个键,然后将其删除,则会导致该错误。 Ctrl 单击 View Controller 的文件所有者并从那里删除缺少的键。

关于objective-c - 此类与键错误的键值编码不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17475750/

相关文章:

excel - 即使发生错误处理,Excel错误对话框仍会弹出

java - Spring 验证错误的文档

从 jar 文件加载类时出现 java.lang.ClassNotFoundException

C# DLL 运行时错误 - 构建、运行,但在尝试使用 DLL 函数时抛出未处理的异常

ios - 使用 Objective c 创建一个文件名不存在于该文件夹中的文件

javascript - UIWebView 保存用户名和密码

ios - 如果更改基线偏移属性,UILabel 属性文本不能有多行

iphone - 如何在 UIView 的深层层次结构中检测 UIImageView 上的触摸事件?

c# - 如何将 JavaScript 错误从操作传递到 View ?

jsf - 在运行时从支持 bean 添加 JSF 2 复合组件