由于未捕获的异常“NSUnknownKeyException",iOS 终止应用程序 [英] iOS Terminating app due to uncaught exception 'NSUnknownKeyException'

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

问题描述

我对objective c 和iOS 编程还很陌生,我遇到了这个非常奇怪的错误.有问题的应用程序使用我使用 NSObject 创建的自定义类型的一组预设值初始化 NSMutableArray.这是由应用程序操纵的.如果在应用程序运行时添加了新值,它们将使用 NSUserDefaults 保存,并在下一个应用程序打开时从 NSUserDefaults 与默认值一起调出.

I am pretty new to objective c and iOS programming, and I have this pretty strange error. The app in question initializes a NSMutableArray with a preset set of values of a custom type I made using NSObject. Which is manipulated by the app. If new values are added during app run time, they are saved using NSUserDefaults, and are brought up from NSUserDefaults along with the default values on next app open.

这是我得到的错误:

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFConstantString 0xb404> valueForUndefinedKey:]: this class is not key value coding-compliant for the key score.'
*** First throw call stack:
(0x1c9b012 0x10d8e7e 0x1d23fb1 0xb84d1d 0xaf100b 0xaf0fbd 0xb0f247 0xb3023c 0xb30056 0x3e40 0x3c5f 0x11f5ad 0x10ec705 0x202c0 0x20258 0x242ff4 0x10ec705 0x202c0 0x20258 0xe1021 0xe157f 0xe1056 0x246af9 0x10ec705 0x202c0 0x20258 0xe1021 0xe157f 0xe06e8 0x4fcef 0x4ff02 0x2dd4a 0x1f698 0x1bf6df9 0x1bf6ad0 0x1c10bf5 0x1c10962 0x1c41bb6 0x1c40f44 0x1c40e1b 0x1bf57e3 0x1bf5668 0x1cffc 0x290d 0x2835)
libc++abi.dylib: terminate called throwing an exception

我不太确定错误是什么或如何调试它.

I am not quite sure what the error is or how to go about debugging it.

之前这段代码完美无缺,我所做的只是从预设的默认元素列表中删除一两个元素,并在模拟器中模拟删除应用程序,并重新编译代码.自从我的程序崩溃后,出现上述消息,我不知道如何修复它.

Previously this code worked flawlessly, all I did was remove one or two elements from the preset default list of elements, and in the simulator, simulated deleting the app, and recompiled the code. Ever since my program crashes, with the above message, and I can't figure out how to fix it.

因此,如果有人可以就如何调试此问题给我一些帮助,那就太好了.我可以根据需要附加代码,我不确定哪些代码与显示相关,并且发布项目中涉及的所有代码可能太多了.

So if someone can give me some help on how to go about debugging this, that would be wonderful. I can attach code as needed, i'm not sure what code would be relevant to be shown, and it may be too much to post all the code involved in the project.

用于编码和解码名为 name.h 的自定义 Name NSObject 类的属性的代码:

Code to encode and decode the properties of my custom Name NSObject class called name.h:

-(void)encodeWithCoder:(NSCoder *)encoder
{
    [encoder encodeObject:self.name forKey:@"name"];
    [encoder encodeInteger:self.score forKey:@"score"];

}

-(id)initWithCoder:(NSCoder *)decoder
{
    if((self = [super init]))
    {
        self.name = [decoder decodeObjectForKey:@"name"];
        self.score = [decoder decodeIntegerForKey:@"score"];
    }

    return self;
}

从类中检索数据,以防万一,这段代码出现在 appdelegate.m 中:

Retrieving Data from class, incase this matters, this code occurs in appdelegate.m:

NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"dataArray"];


NSInteger score = 0;


NSMutableArray *temp = [[NSMutableArray alloc] initWithObjects:@"name", nil];

NSMutableArray *tempList = [[NSMutableArray alloc] init];

for(NSString *y in temp)
{
    Name *name = [[Name alloc] init];
    name.name  = y;
    name.score = score;
    [tempList addObject:name];


}

if (data)
{

    NSArray *list = [NSKeyedUnarchiver unarchiveObjectWithData:data];
   NSMutableArray *names = [[NSMutableArray alloc]initWithArray:list];
  // [_nameList addObjectsFromArray:temp];
    NSMutableArray *t = [[names arrayByAddingObjectsFromArray:tempList ] mutableCopy];

    _nameList = [[NSMutableArray alloc]init];

    [_nameList addObjectsFromArray:t];


}
else 
{
    //First time load or data is not saved yet
    _nameList = [[NSMutableArray alloc] initWithObjects:@"name", nil];

}

在关闭时保存数组:

- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
NSData *data =[NSKeyedArchiver archivedDataWithRootObject:_nameList];


[[NSUserDefaults standardUserDefaults] setObject:data forKey:@"dataArray"];
}

相同的代码在 applicationDidEnterBackground 中.

same code is in applicationDidEnterBackground.

按分数"排序的代码

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:    (UIViewController *)viewController
{


    if(viewController == _viewController3)
    {
        [self sortNames:_nameList];
        [[(ThirdViewController*)_viewController3 topList] reloadData];

    }
}

-(void)sortNames:(NSMutableArray*)test
{



NSArray* temp = [[NSArray alloc] initWithArray:test];


NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"score" ascending:NO];
NSArray *sortedLinks = [[temp sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]] mutableCopy];

_nameList = (NSMutableArray*) sortedLinks;
}

推荐答案

这是我的两分钱.你有这条线:

Here's my two cents. You have this line:

[encoder encodeObject:self.name forKey:@"name"];

然后是这一行:

Name *name = [[Name alloc] init];

这让我觉得self.name"属性是这些Name"自定义子类之一.

This makes me think that the "self.name" property is one of these "Name" custom subclasses.

我相信,如果您创建了一个自定义子类并且希望它encodewithcoder,您必须将encodewithcoder 方法添加到您的自定义子类中并让它encodewithcoder 尽可能原始地编码它的所有属性和实例变量.

I believe that if you make a custom subclass and you want it to encodewithcoder, you have to add the encodewithcoder method to your custom subclass and have it encodewithcoder all of its properties and instance variables as primitively as you can.

意思是,您的 Name 类需要有自己的 encodewithcoder 方法,该方法可以对存储为工厂对象或 c 原语的所有属性和实例变量进行编码.

Meaning, your Name class needs to have its own encodewithcoder method that encodes all of its properties and instance variables that have been stored as factory objects or c primitives.

我还是个新手,很重视我的代表.如果我错了,请评论,我会删除,但请不要低估我的遗忘

I'm still pretty new and value my rep. If I'm wrong, please comment and I'll delete but please don't downvote me to oblivion

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

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