由于未捕获的异常"NSUnknownKeyException"而终止应用程序-错误 [英] Terminating app due to uncaught exception 'NSUnknownKeyException' - error

查看:226
本文介绍了由于未捕获的异常"NSUnknownKeyException"而终止应用程序-错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在遵循《 Big Nerd Ranch iOS编程指南》(第3版),并遵循其所说的一切来创建该项目.我遇到了一个错误,但我不知道如何解决,因为我是iOS的新手.当我从Xcode的模板中使用基于单视图的应用程序时,我的程序最初出现问题,给我这个错误:

2012-05-25 16:02:12.926 Whereami [1083:707]应用程序窗口预期在应用程序启动结束时具有根视图控制器

扫描了一些论坛后,我发现我需要在Xcode中设置MainInterface:

之前- https://i.imgur.com/YCnRM.png

之后- https://i.imgur.com/5QnJ3.png

通过在设置中将xib文件名添加到MainInterface文本框中来修复上述错误后,我收到此错误:

    2012-05-25 16:04:09.068 Whereami[1102:707] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIApplication 0x11f620> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key activityIndicator.
*** First throw call stack:
(0x3507b88f 0x364a2259 0x3507b5c5 0x30eb6323 0x30eb5e23 0x30e8ff09 0x34fda7d3 0x34fdb461 0x323111af 0x3231294d 0x32248509 0x320d1893 0x320cb8d7 0x32099c6b 0x3209970f 0x320990e3 0x362ce22b 0x3504f523 0x3504f4c5 0x3504e313 0x34fd14a5 0x34fd136d 0x320caa13 0x320c7e7d 0x7ebd7 0x7eb7c)
terminate called throwing an exception

这是我的源文件:

AppDelegate.m- https://i.imgur.com/7FdCZ.png

WhereamiViewController.h WhereamiViewController.m

查看- https://i.imgur.com/IZeoa.png

运行- https://i.imgur.com/dyYKd.png

我很困惑,需要弄清楚这一点,以便我继续前进并最终完成这本书.我需要能够在下周之前完成我的第一个应用程序.

解决方案

问题文本似乎是您在问这个问题... 一种设置方式如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    LoginViewController* loginView = [[[LoginViewController alloc]initWithNibName:nil bundle:nil]autorelease];
    mainNavController = [[UINavigationController alloc]initWithRootViewController:loginView];

    //here is the magic line
    [_window addSubview:mainNavController.view];

    [self.window makeKeyAndVisible];
    return YES;
}

或者您也可以不使用mainNavController:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
        // Override point for customization after application launch.
    LoginViewController* loginView = [[[LoginViewController alloc]initWithNibName:nil bundle:nil]autorelease];

    [_window addSubview:loginView.view];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

标题似乎是您在问这个问题... 现在,当您尝试访问不存在或拼写错误的选择器或类的成员时,通常会出现NSUnknownKeyException错误(通常对我来说是后者!).您可以想到类,如字典,调用成员或函数,就像通过键要求字典对象一样. 例

`@selector(misspelledFunctionName:)` 

[object functionThatIsMisspelledOrNotDefinedInObject];

我不确定要问哪个问题,所以我试图回答这两个问题.希望这会有所帮助:)

这些最终都是答案,或者仍然是答案的一部分. UIApplication没有分配正确的视图,因此xib发送的键不存在并导致崩溃.我从plist中删除了根视图分配(结果没有崩溃,但出现黑屏),然后像上面一样分配了视图(该视图显示完美!).

I am following the Big Nerd Ranch iOS Programming Guide(3rd Edition), and have followed everything it says for creating this project. I am getting an error I do not know how to fix though, as I'm new to iOS. I was initially having a problem with my program giving me this error when I used Single-View Based Application from the template in Xcode:

2012-05-25 16:02:12.926 Whereami[1083:707] Application windows are expected to have a root view controller at the end of application launch

After scanning some forums I found out I need to set my MainInterface in Xcode:

Before - https://i.imgur.com/YCnRM.png

After - https://i.imgur.com/5QnJ3.png

After fixing the error above by adding the xib filename to the MainInterface text box in the settings I am getting this error:

    2012-05-25 16:04:09.068 Whereami[1102:707] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIApplication 0x11f620> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key activityIndicator.
*** First throw call stack:
(0x3507b88f 0x364a2259 0x3507b5c5 0x30eb6323 0x30eb5e23 0x30e8ff09 0x34fda7d3 0x34fdb461 0x323111af 0x3231294d 0x32248509 0x320d1893 0x320cb8d7 0x32099c6b 0x3209970f 0x320990e3 0x362ce22b 0x3504f523 0x3504f4c5 0x3504e313 0x34fd14a5 0x34fd136d 0x320caa13 0x320c7e7d 0x7ebd7 0x7eb7c)
terminate called throwing an exception

Here are my source files:

AppDelegate.m - https://i.imgur.com/7FdCZ.png

WhereamiViewController.h WhereamiViewController.m

View - https://i.imgur.com/IZeoa.png

Run- https://i.imgur.com/dyYKd.png

I am stumped, and need to figure this out so I can move on and eventually finish this book. I need to be able to complete my first app for work by next week.

解决方案

The question text seems like you are asking this... One way you can set it is like this:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    LoginViewController* loginView = [[[LoginViewController alloc]initWithNibName:nil bundle:nil]autorelease];
    mainNavController = [[UINavigationController alloc]initWithRootViewController:loginView];

    //here is the magic line
    [_window addSubview:mainNavController.view];

    [self.window makeKeyAndVisible];
    return YES;
}

Or you can do it without the mainNavController:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
        // Override point for customization after application launch.
    LoginViewController* loginView = [[[LoginViewController alloc]initWithNibName:nil bundle:nil]autorelease];

    [_window addSubview:loginView.view];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

The title seems like your asking this... Now the NSUnknownKeyException error usually crops up when you try to access a selector or a member of a class that does not exist or is spelled wrong (Usually the latter for me!). You can think of classes like a dictionary and calling a member, or function, is like asking for a dictionary object by it's key. Ex.

`@selector(misspelledFunctionName:)` 

or

[object functionThatIsMisspelledOrNotDefinedInObject];

I am unsure which question is being asked so I tried to answer both. Hopefully that will help :)

EDIT: These both ended up being the answer, or part of it anyways. The UIApplication did not have the correct view assigned to it so xib was sending keys that did not exist and caused it to crash. I removed the root view assignment from the plist (result no crashes but blank screen) then assigned the view like the above (the view displayed perfectly!).

这篇关于由于未捕获的异常"NSUnknownKeyException"而终止应用程序-错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆