此类与键错误的键值编码不兼容
Posted
技术标签:
【中文标题】此类与键错误的键值编码不兼容【英文标题】:this class is not key value coding-compliant for the key error 【发布时间】:2013-07-04 17:37:28 【问题描述】:我的问题是当我构建应用程序时出现此错误
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
老实说,我已经尝试了我所知道的所有可能的解决方案。我检查了与我的视图的所有连接,它们都很好。提前谢谢你。
【问题讨论】:
为什么//- (IBAction)hourTimeOutOfBraceChanged:(id)sender;
被注释掉了?您的操作方法具有选择器“hourTimeOutOfBraceChanged:”(带有尾随冒号),但错误消息抱怨“hourTimeOutOfBraceChanged”(不带冒号),因此其中一个连接与方法选择器不完全匹配。
试过 r̶e̶b̶o̶o̶t̶i̶n̶g̶ 删除派生数据?
哦,对不起,我不是要注释掉它,当我收到错误时,我只是在尝试不同的方法来解决问题
【参考方案1】:
如果您在接口构建器中为该缺失键的某些 UILabel 实例提供了一个键,然后将其删除,则会导致该错误。 Ctrl 单击视图控制器的文件所有者并从那里删除缺少的键。
【讨论】:
谢谢伙计,这个工作我有一些东西与一个不存在的对象相关联。【参考方案2】:看起来您正在创建一个带有 nib 文件的 FirstViewController 对象。 你可能正在做类似的事情:
[[[FirstViewController alloc] initWithNibName:@"AnOtherXibFile" bundle:nil] autorelease];
这意味着它将创建您喜欢的 FirstViewController 对象,以便将视图编译到您的 AnOtherXibFile.xib 文件中。
您能否仔细检查您创建的 FirstViewController 是否使用正确的 xib 文件? 你应该有类似的东西:
[[[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil] autorelease];
或者,如果您使用正确的 xib,请仔细检查对您的 ViewController 的所有引用...
【讨论】:
以上是关于此类与键错误的键值编码不兼容的主要内容,如果未能解决你的问题,请参考以下文章
如何修复错误:此类与键 tableView 的键值编码不兼容。 [复制]
iOS/Facebook 登录错误:此类与键的键值编码不兼容