IOS 学习笔记 “this class is not key value coding-compliant for the xxx”问题的解决

这篇博客主要介绍了在iOS开发中遇到'not key value coding-compliant for the key'错误的三种常见情况及其解决办法。包括IBOutlet重命名、xib文件File's Owner类配置错误以及AppDelegate中的初始化问题。对于初学者,这些解决方案提供了排查此类问题的指引。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

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

第一种:IBOutlet重命名了或者是Inteface Builder中的连线有两个

第二种:xib文件的File's Owner对应的Class文件错误

第三种:AppDelegate


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

在学习的过程中,对于初学者这个问题肯定会遇到这个错误“this class is not key value coding-compliant for the key”,但是根据网上的解决办法基本上都只是前两种解决方案:

 

第一种:IBOutlet重命名了或者是Inteface Builder中的连线有两个

这种可能性是很大的

 

第二种:xib文件的File's Owner对应的Class文件错误

其实这种情况如果是在创建ViewController文件的时候勾选了同时创建xib,是不会有这个问题的,但是这里是一个检查点

第三种:AppDelegate

这种情况是最隐蔽的,对于初学者来说,先看一段代码:

 
 
#import "AppDelegate.h"
 
@interface AppDelegate ()
 
@end
 
@implementation AppDelegate
 
 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    //最后一步将创建xib文件绑定到window界面上
    self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = [[UIViewController alloc]initWithNibName:@"RootViewController" bundle:nil];
    [self.window makeKeyAndVisible];
    return YES;
}
 
 
@end

这段代码是在去掉故事板使用xib布局的时候需要修改的地方,但是对于初学者来说,可能并不知道这是有问题的,而且如果遇到问题,还真是不好解决,会以为这里不是通过UIViewController alloc]initWithNibName:来加载RootViewController吗?没有问题啊,但是,就是因为这个问题造成了:“this class is not key value coding-compliant for the key”错误,原因就是:我们需要初始化的是RootViewController,而不是系统的UIViewController这个所有ViewController的顶级父类,因此改为一下代码就好了:

 
 
#import "AppDelegate.h"
 
@interface AppDelegate ()
 
@end
 
@implementation AppDelegate
 
 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    //最后一步将创建xib文件绑定到window界面上
    self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = [[RootViewController alloc]initWithNibName:@"RootViewController" bundle:nil];
    [self.window makeKeyAndVisible];
    return YES;
}
 
 
@end

将UIViewController修改为RootViewController

 

 

 

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值