xcode 由于未捕获的异常“NSUnknownKeyException”而终止应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8232766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Terminating app due to uncaught exception 'NSUnknownKeyException'
提问by Jon
I'm using a tutorial to write a fairly simple app. I copied the app to my work computer today. It was working perfectly on my laptop last night, but today I get this error. I tried gutting nearly all of the code, and even deleted everything in the didFinishLaunchingWithOptions function, but I still get this error. I don't even have a navBar declared anywhere! I assume there's some kind of project or file setting outside of the code that is the problem?
我正在使用教程来编写一个相当简单的应用程序。我今天将该应用程序复制到我的工作计算机上。昨晚它在我的笔记本电脑上运行良好,但今天我收到了这个错误。我尝试删除几乎所有代码,甚至删除了 didFinishLaunchingWithOptions 函数中的所有内容,但我仍然收到此错误。我什至没有在任何地方声明导航栏!我认为代码之外有某种项目或文件设置是问题所在?
2011-11-22 14:39:32.294 LetsEat[15320:b603] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<LetsEatAppDelegate 0x5a824a0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key navBar.'
*** Call stack at first throw:
(
0 CoreFoundation 0x00fa45a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x010f8313 objc_exception_throw + 44
2 CoreFoundation 0x00fa44e1 -[NSException raise] + 17
3 Foundation 0x0079f677 _NSSetUsingKeyValueSetter + 135
4 Foundation 0x0079f5e5 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 285
5 UIKit 0x0021b30c -[UIRuntimeOutletConnection connect] + 112
6 CoreFoundation 0x00f1a8cf -[NSArray makeObjectsPerformSelector:] + 239
7 UIKit 0x00219d23 -[UINib instantiateWithOwner:options:] + 1041
8 UIKit 0x0021bab7 -[NSBundle(UINSBundleAdditions) loadNibNamed:owner:options:] + 168
9 UIKit 0x0002117a -[UIApplication _loadMainNibFile] + 172
10 UIKit 0x00021cf4 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 291
11 UIKit 0x0002c617 -[UIApplication handleEvent:withNewEvent:] + 1533
12 UIKit 0x00024abf -[UIApplication sendEvent:] + 71
13 UIKit 0x00029f2e _UIApplicationHandleEvent + 7576
14 GraphicsServices 0x011dd992 PurpleEventCallback + 1550
15 CoreFoundation 0x00f85944 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
16 CoreFoundation 0x00ee5cf7 __CFRunLoopDoSource1 + 215
17 CoreFoundation 0x00ee2f83 __CFRunLoopRun + 979
18 CoreFoundation 0x00ee2840 CFRunLoopRunSpecific + 208
19 CoreFoundation 0x00ee2761 CFRunLoopRunInMode + 97
20 UIKit 0x000217d2 -[UIApplication _run] + 623
21 UIKit 0x0002dc93 UIApplicationMain + 1160
22 LetsEat 0x00001c99 main + 121
23 LetsEat 0x00001c15 start + 53
)
terminate called throwing an exceptionCurrent language: auto; currently objective-c
(gdb)
EDIT: This is the top of my delegate.m file. As you can see it's all boilerplate:
编辑:这是我的 delegate.m 文件的顶部。正如你所看到的,都是样板文件:
#import "LetsEatAppDelegate.h"
//#import "ItemsViewController.h"
@implementation LetsEatAppDelegate
@synthesize window = _window;
@synthesize managedObjectContext = __managedObjectContext;
@synthesize managedObjectModel = __managedObjectModel;
@synthesize persistentStoreCoordinator = __persistentStoreCoordinator;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
[self.window makeKeyAndVisible];
return YES;
}
I removed every other file in the project and still get this error.
我删除了项目中的所有其他文件,但仍然出现此错误。
EDIT 2
编辑 2
As it now stands, I only get this error on my work machine. The exact same files run perfectly on my home laptop. Same version of XCode (4.2.1)
就目前而言,我只在我的工作机器上收到此错误。完全相同的文件在我的家用笔记本电脑上完美运行。相同版本的 XCode (4.2.1)
采纳答案by Jon
Fixed - went to iOS Simulator > Reset Content and Setting
已修复 - 转到 iOS 模拟器 > 重置内容和设置
回答by Nitin Alabur
Reset Content and Setting didn't work.
重置内容和设置不起作用。
Here's what wasted 2 hrs of my late night time:
这是浪费了我 2 小时的深夜时间:
I had connected an UIControl outlet in the interface builder to the IBOutlet in the xib's owner. For some reason, the IBOutlet was deleted from the owner, but the reference to the outlet remained dangling in the xib. This would always give me the error
我已将界面构建器中的 UIControl 插座连接到 xib 所有者中的 IBOutlet。出于某种原因,IBOutlet 从所有者中删除,但对出口的引用仍然悬在 xib 中。这总是会给我错误
"Terminating app due to uncaught exception 'NSUnknownKeyException'"
Lesson learnt: When deleting any outlets for vars in the implementation, make sure to unhook the respective connection in the IB
经验教训:在实现中删除 vars 的任何出口时,请确保在 IB 中解开相应的连接
Update: (26 Jan 2015)
更新:(2015 年 1 月 26 日)
There's a good reason why the Interface Builder or the nibs behave this way, and I realized the why, today.
Interface Builder 或笔尖的行为是有充分理由的,今天我意识到了原因。
If you have an IBOutlet in TheBaseClass, and you have a nib or scene in the storyboard for a TheSubClass, there are two ways (hacks) available for you to connect this outlet:
如果您在 TheBaseClass 中有一个 IBOutlet,并且您在故事板中有一个 TheSubClass 的笔尖或场景,则有两种方法(技巧)可用于连接此插座:
Under the Identity Inspector, name the "Custom Class" identifier to TheBaseClass, connect the outlet, then set the Class name in the "Custom Class" section to TheSubclass
Add the IBOutlet in your TheSubclass code, connect it to the ui element in the nib, add the IBOutlet code in TheBaseClass, delete that code in TheSubClass
在身份检查器下,将“自定义类”标识符命名为 TheBaseClass,连接插座,然后将“自定义类”部分中的类名设置为 TheSubclass
在 TheSubclass 代码中添加 IBOutlet,将其连接到笔尖中的 ui 元素,在 TheBaseClass 中添加 IBOutlet 代码,在 TheSubClass 中删除该代码
When I came across this answerit just fell in place, as to why Apple would let this dangling reference problem remain as is. Because, this is not a bug. This is a feature.
当我遇到这个答案时,它就到位了,至于为什么 Apple 会让这个悬而未决的参考问题保持原样。因为,这不是错误。这是一个特点。
回答by RachelD
I had this issue and found that I had set the 'Main Interface' and it was causing an issue.
我遇到了这个问题,发现我已经设置了“主界面”,这导致了一个问题。
To change this setting click on your project (in the left hand file browser). The center area should now display the 'Summary' of settings for your project. This is the same area where you would change the iOS Application Target or the Supported Device Orientations. Under the section 'iPhone/ iPod Deployment Info' clear the 'Main Interface' drop down, leaving it blank. This fixed the issue for me.
要更改此设置,请单击您的项目(在左侧文件浏览器中)。中心区域现在应显示项目设置的“摘要”。这与您将更改 iOS 应用程序目标或支持的设备方向的区域相同。在“iPhone/iPod 部署信息”部分下,清除“主界面”下拉菜单,将其留空。这为我解决了这个问题。
Good luck.
祝你好运。
回答by Jesse Black
The issue is in your nib file. There is an old connection from the LetsEatAppDelegate
to the navBar.
问题出在您的 nib 文件中。从LetsEatAppDelegate
到导航栏有一个旧的连接。
I am not sure how your app is set up, there may be an LetsEatAppDelegate
object in one of your nib files, or there could be a file owner with the class set to LetsEatAppDelegate
我不确定您的应用程序是如何设置的,LetsEatAppDelegate
您的一个 nib 文件中可能有一个对象,或者可能有一个文件所有者的类设置为LetsEatAppDelegate
4 Foundation 0x0079f5e5 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 285
5 UIKit 0x0021b30c -[UIRuntimeOutletConnection connect] + 112
6 CoreFoundation 0x00f1a8cf -[NSArray makeObjectsPerformSelector:] + 239
7 UIKit 0x00219d23 -[UINib instantiateWithOwner:options:] + 1041
8 UIKit 0x0021bab7 -[NSBundle(UINSBundleAdditions) loadNibNamed:owner:options:] + 168
2011-11-22 14:39:32.294 LetsEat[15320:b603] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<LetsEatAppDelegate 0x5a824a0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key navBar.'
*** Call stack at first throw:
回答by user2076367
Oh man. I was having the same problem after removing @properties and/or IBActions and it was driving me crazy! Warkst was right on the money. Go to the problem view controller, click on the orange ball and deselect any dangling references you find. You may need to scroll down to find them all. This solved my problem. Thanks a million.
天啊。删除@properties 和/或 IBActions 后我遇到了同样的问题,这让我发疯了!沃克斯特在钱上是对的。转到问题视图控制器,单击橙色球并取消选择您找到的任何悬空引用。您可能需要向下滚动才能找到它们。这解决了我的问题。太感谢了。
回答by frank
I meet this problem too,
我也遇到这个问题
1.In AppDelegate method
1.在AppDelegate方法中
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
the rootViewController is init by this methods initWithNibName..... but actually the nib file is not exist, so the exception throw..
rootViewController 是通过这个方法 initWithNibName ..... 初始化的,但实际上 nib 文件不存在,所以异常抛出..
2.When viewController load view it crashes
2.当viewController加载视图时崩溃
I guess maybe because I deleted the xib file,then the view controller still init with nib file.. so I add the -(void)load view methods. and it is ok now..ps:I reset the simulator,but it doesn't work.
我想可能是因为我删除了 xib 文件,然后视图控制器仍然使用 nib 文件进行初始化..所以我添加了 -(void)load 视图方法。现在可以了..ps:我重置了模拟器,但它不起作用。
Hope this help.
希望这有帮助。
回答by Quentin Malgaud
I faced the same problem recently. All my outlet connections where perfectly fine though. I finally made it by making decent CLEAN of the project. Thanks to @Robert Wohnoutka and this post: How to Delete Derived Data and Clean Project in Xcode 5 and Xcode 6?
我最近遇到了同样的问题。我所有的插座连接都非常好。我终于通过对项目进行了体面的 CLEAN 实现了它。感谢@Robert Wohnoutka 和这篇文章:How to Delete Derived Data and Clean Project in Xcode 5 and Xcode 6?
回答by Robert Wohnoutka
Clearing "Main Interface" also solved the issue for me. You may need to perform a "Clean" after clearing "Main Interface" and before building.
清除“主界面”也为我解决了这个问题。您可能需要在清除“主界面”之后和构建之前执行“清除”。