由于未捕获异常 'NSUnknownKeyException',应用程序崩溃:iOS 应用程序崩溃。

10

我是一名初学者程序员,正在开发iPhone应用程序。我想知道是否有人能帮我解决我的应用程序中的一个错误。在使用XCode编程时,我发现在添加了更多的代码后,我的应用程序突然在启动时崩溃了。输出面板显示以下错误:

* Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key play.' * First throw call stack: (0x1c91012 0x10cee7e 0x1d19fb1 0xb7ae41 0xafc5f8 0xafc0e7 0xb26b58 0x230019 0x10e2663 0x1c8c45a 0x22eb1c 0xf37e7 0xf3dc8 0xf3ff8 0xf4232 0x433d5 0x4376f 0x43905 0x8eceab6 0x4c917 0x1096c 0x1194b 0x22cb5 0x23beb 0x15698 0x1becdf9 0x1becad0 0x1c06bf5 0x1c06962 0x1c37bb6 0x1c36f44 0x1c36e1b 0x1117a 0x12ffc 0x222d 0x2155) libc++abi.dylib: terminate called throwing an exception (lldb)

应用程序终止时出现的代码如下:

#import <UIKit/UIKit.h>
#import "CIAAppDelegate.h"

int main(int argc, char *argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([CIAAppDelegate class]));
    }
}

我的 ViewController.h 显示:

#import <UIKit/UIKit.h>

@interface CIAViewController : UIViewController {
    IBOutlet UILabel *cruzia;
    IBOutlet UILabel *textarea;
    IBOutlet UIButton *playbtn;
    IBOutlet UIButton *tutorialbtn;
    IBOutlet UIButton *optionsbtn;
    IBOutlet UIButton *trainingbtn;
    IBOutlet UIButton *back;
}

-(IBAction)press;
-(IBAction)press2;
-(IBAction)press3;
-(IBAction)press4;
-(IBAction)press5;

还有我的ViewController.m展示

#import "CIAViewController.h"

@interface CIAViewController ()
@end

@implementation CIAViewController

-(IBAction)press {
    cruzia.hidden = 0;
    textarea.hidden = 0;
    playbtn.hidden = 1;
    tutorialbtn.hidden = 1;
    optionsbtn.hidden = 1;
    trainingbtn.hidden = 1;
    back.hidden = 0;
    cruzia.text = @"Play";
    textarea.text = @"Hello! You are playing the game of Cruzia!";
}

-(IBAction)press2 {
    cruzia.hidden = 0;
    textarea.hidden = 0;
    playbtn.hidden = 1;
    tutorialbtn.hidden = 1;
    optionsbtn.hidden = 1;
    trainingbtn.hidden = 1;
    back.hidden = 0;
    cruzia.text = @"Tutorial";
    textarea.text = @"Welcome! You are watching the Cruzia tutorial!";
}

-(IBAction)press3 {
    cruzia.hidden = 0;
    textarea.hidden = 0;
    playbtn.hidden = 1;
    tutorialbtn.hidden = 1;
    optionsbtn.hidden = 1;
    trainingbtn.hidden = 1;
    back.hidden = 0;
    cruzia.text = @"Options";
    textarea.text = @"You are now in the Options screen. You can edit the settings of the game!";
}

-(IBAction)press4 {
    cruzia.hidden = 0;
    textarea.hidden = 0;
    playbtn.hidden = 1;
    tutorialbtn.hidden = 1;
    optionsbtn.hidden = 1;
    trainingbtn.hidden = 1;
    back.hidden = 0;
    cruzia.text = @"Training";
    textarea.text = @"This is the training area. You can improve your Cruzia skills here!";
}

-(IBAction)press5 {
    cruzia.hidden = 0;
    textarea.hidden = 0;
    playbtn.hidden = 0;
    tutorialbtn.hidden = 0;
    optionsbtn.hidden = 0;
    trainingbtn.hidden = 0;
    back.hidden = 1;
    cruzia.text = @"Cruzia";
}


- (void)viewDidLoad
{
    textarea.hidden = 1;
    playbtn.hidden = 0;
    tutorialbtn.hidden = 0;
    optionsbtn.hidden = 0;
    trainingbtn.hidden = 0;
    back.hidden = 1;
    cruzia.text = @"Cruzia";
    [super viewDidLoad];
}

@end

很抱歉浪费您的时间,但我遇到这个错误已经有一段时间了,希望您能给出解决方案。


3
问题出在xib或storyboard文件中。您可能有一个项目(可能是UIButton)与“play”链接,但您在代码中将该项目重命名为“playbtn”,而没有重新分配xib中的outlet。检查xib/storyboard中的播放按钮,并删除其不存在的outlet。 - Matthias Bauch
嘿,又是这个家伙 http://stackoverflow.com/questions/17538555/why-is-my-app-crashing-with-terminating-app-due-to-uncaught-exception-nsunknown - Matt S.
1
它告诉你中央情报局不是闹着玩的。 - Hot Licks
1个回答

45

问题在哪里:

在运行时,您的视图控制器将从其XIB/Storyboard反序列化。在此过程中,它将读取IB文件中的所有对象,并使用NSCoder开始实例化这些对象。当它在您的视图中实例化这些对象时,它将阅读Storyboard中的IBOutletIBAction连接,并开始将液态化的对象分配给在设计时连接的这些属性,以便您在运行时具有编程访问权限。但是问题在于:如果您的连接指向不存在的属性,则会抛出异常,因为它被告知该属性存在,但实际上并不存在。

问题在于您的Storyboard/XIB期望您的类具有一个名为play的属性。正如您的接口显示的那样,您没有这样的属性。我猜想您曾经有这样一个属性,但是已将其重命名为playbtn

这就是所谓的

此类不符合键值编码-compliant for the key play

这意味着它在幕后尝试执行以下操作之类的操作:

[self setValue:button forKey:@"play"]

如需了解更多关于Key/Value Coding和序列化方面的知识,请舒适地坐下来阅读Key Value Coding Programming GuideArchives and Serializations Programming Guide

如何修复:

在Interface Builder中,右键单击您的视图控制器并查看连接列表:

enter image description here

看看有一个名为play的属性连接,并且它旁边有一个小黄三角形?这是Interface Builder试图警告您设置了一个连接,但标题没有显示该名称的IBOutlet

你只需要点击那个小的x来断开到缺失属性的连接即可。

如何预防这种情况

每当您在代码中更改属性时,都需要确保断开与Interface Builder中任何相关联的连接。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接