Ios Xcode-如何修复';NSUnknownKeyException';,原因:…;此类不符合密钥X的键值编码;错误?
我正在尝试将Ios Xcode-如何修复';NSUnknownKeyException';,原因:…;此类不符合密钥X的键值编码;错误?,ios,macos,cocoa,cocoa-touch,interface-builder,Ios,Macos,Cocoa,Cocoa Touch,Interface Builder,我正在尝试将UILabel与类中创建的IBOutlet链接 我的应用程序因以下错误而崩溃 这是什么意思 我怎样才能修好它 ***由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[setValue:forUndefinedKey:]:此类不符合密钥XXX的键值编码。” 您只需指定IBOutlet一次,您的ivar不需要IBOutlet标签 您是否正在使用UIViewController实例化NIB?在某个时候,您应该调用[SecondView initWith
UILabel
与类中创建的IBOutlet
链接
我的应用程序因以下错误而崩溃
这是什么意思
我怎样才能修好它
***由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[setValue:forUndefinedKey:]:此类不符合密钥XXX的键值编码。”
IBOutlet
一次,您的ivar不需要IBOutlet
标签UIViewController
实例化NIB?在某个时候,您应该调用[SecondView initWithNibName:@“yourNibName”bundle:nil]代码>
IBOutlet
一次,您的ivar不需要IBOutlet
标签UIViewController
实例化NIB?在某个时候,您应该调用[SecondView initWithNibName:@“yourNibName”bundle:nil]代码>
main窗口中的Second
视图控制器引起的。xib
具有UIViewController
类,而不是SecondView
。更改为正确的类可以解决此问题
顺便说一句,在Objective-C中使用类似“string”的名称是一种不好的做法。它会引发运行时命名冲突。即使在一次性的练习应用程序中也要避免使用它们。命名冲突可能很难追踪,您不想浪费时间
此错误的另一个可能原因是:当将元素从一个控制器复制和粘贴到另一个控制器时,Xcode会以某种方式保留到原始控制器的链接,即使在编辑此元素并将其重新链接到新控制器之后也是如此
此错误的另一个可能原因:
坏的出口
您已经删除或重命名了.h
文件中的一个插座名称
在.xib
或.storyboard
文件的连接检查器中删除它
还有一个可能的原因
(在我的例子中)使用可绑定属性扩展UIView,并设置这些可绑定属性的值(即阴影、角半径等),然后从UIView扩展中删除这些属性(出于某种原因),但以下
仍保留在xml中(属于foo.storyboard
):
解决方案:右键单击foo.storyboard
>作为源代码打开>按键路径搜索(即阴影半径)>删除导致问题的
视图控制器可能在xib中具有错误的类。
我下载了你的项目
您得到的错误是
“NSUnknownKeyException”,原因:“[setValue:forUndefinedKey:]:此类不符合密钥字符串的键值编码。”
这是由main窗口中的Second
视图控制器引起的。xib
具有UIViewController
类,而不是SecondView
。更改为正确的类可以解决此问题
顺便说一句,在Objective-C中使用类似“string”的名称是一种不好的做法。它会引发运行时命名冲突。即使在一次性的练习应用程序中也要避免使用它们。命名冲突可能很难追踪,您不想浪费时间
此错误的另一个可能原因是:当将元素从一个控制器复制和粘贴到另一个控制器时,Xcode会以某种方式保留到原始控制器的链接,即使在编辑此元素并将其重新链接到新控制器之后也是如此
此错误的另一个可能原因:
坏的出口
您已经删除或重命名了.h
文件中的一个插座名称
在.xib
或.storyboard
文件的连接检查器中删除它
还有一个可能的原因
(在我的例子中)使用可绑定属性扩展UIView,并设置这些可绑定属性的值(即阴影、角半径等),然后从UIView扩展中删除这些属性(出于某种原因),但以下
仍保留在xml中(属于foo.storyboard
):
解决方案:右键单击foo.storyboard
>作为源代码打开>按键路径搜索(即阴影半径)>删除导致问题的
,我发现的另一个“不符合”问题是,由于某种原因,我设法获得了一个类的两个副本
我把钥匙加错了。Interface Builder仍然可以看到这些键,并让我连接到它们,但在运行时它使用的是没有新键的类的另一个副本
为了找到哪一个是“正确”的副本,我使用XCode的cmd,单击其他地方的类名跳转到正确的副本,然后我删除了未使用的坏副本(在首先从未使用的副本中进行编辑之后)
这个故事的寓意是:重复的类文件是不好的。我发现的另一个“不兼容”问题是,由于某种原因,我设法获得了一个类的两个副本
我把钥匙加错了。Interface Builder仍然可以看到这些键,并让我连接到它们,但在运行时它使用的是没有新键的类的另一个副本
为了找到哪一个是“正确的”副本,我使用XCode的cmd,点击其他地方的类名跳转到正确的副本,然后我删除了坏的unu
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="color" keyPath="shadowColor">
<color key="value" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="shadowOpacity">
<real key="value" value="50"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="point" keyPath="shadowOffset">
<point key="value" x="5" y="5"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="shadowRadius">
<real key="value" value="16"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="borderWidthValue">
<real key="value" value="0.0"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIViewController 0x18afe0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key welcomeLabel.'
-(void)loadView {
// Ensure that we don't load an .xib file for this viewcontroller
self.view = [UIView new];
}
UIViewController *deviceViewController = [[UIViewController alloc] initWithNibName:@"DeviceViewController" bundle:nil];
DeviceViewController *deviceViewController = [[DeviceViewController alloc] initWithNibName:@"DeviceViewController" bundle:nil];
DeviceViewController
DeviceViewController.h
DeviceViewController.m
"import DeviceViewController.h"
[self presentViewController:deviceViewController animated:YES completion:nil];
BMTester *viewController = [[BMTester alloc] initWithNibName:@"WrongNibName" bundle:nil];
<connections>
<outlet property="mapPostsView" destination="4EV-NK-Bhn" id="ubM-Z6-mwl"/>
<outlet property="mapView" destination="kx6-TV-oQg" id="4wY-jv-Ih6"/>
<outlet property="sidebarButton" destination="6UH-BZ-60q" id="8Yz-5G-HpY"/>
</connections>
UIViewController* controller = [[UIViewController
alloc]initWithNibName:@"TempViewController" bundle:nil];
[self.navigationController pushViewController:controller animated:true];
TempViewController* controller = [[TempViewController
alloc]initWithNibName:@"TempViewController" bundle:nil];
[self.navigationController pushViewController:controller animated:true];