不确定如何处理“由于未捕获异常终止应用程序”NSInvalidArgumentException“ [英] Not sure how to handle "Terminating app due to uncaught exception 'NSInvalidArgumentException'"

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

问题描述

我正在创建一个应用程序,使用我自己创建的类,条目,将特定数据存储在数组中。但是,当我尝试访问存储在Entry对象中的值之一时,我一直遇到此错误。每次提示用户创建其中一个对象时,它将被添加到rawArray,然后存储到[NSUserDefaults standardUserDefaults]中。在这些代码行,程序崩溃:

I'm working on creating an app that uses a class I created myself, Entry, to store specific data in an array. However, I keep running into this error when I try to access one of the values stored in an Entry object. Every time I prompt the user to create one of these objects, it is added to "rawArray," which is in turn stored into [NSUserDefaults standardUserDefaults]. At these lines of code, the program crashes:

Entry *currEntry = [[[NSUserDefaults standardUserDefaults] arrayForKey:@"rawArray"] objectAtIndex:section];
NSLog(@"date: %@", [currEntry getDate]);

我只是试图拉取对应于当前UITableView节的数组元素,然后NSLog 日期变量存储在其中。这里是Entry类的getDate方法:

I'm only trying to pull the array element that corresponds to the current UITableView section, and then NSLog the "date" variable stored in it. Here is the getDate method from the Entry class:

-(NSString *)getDate{
   return date;
}

对象本身只是另一个NSString Entry类,它在类的init中设置。如果任何人可以帮我解决这个问题,我会非常感激。

The date object itself is just another NSString in the Entry class, which is set in the class's init. If anyone can help me sort through this problem, I'd be incredibly thankful.

这是完整的崩溃日志:

2012-04-24 16:31:32.545 Know[7278:f803] -[__NSCFString getDate]: unrecognized selector sent to instance 0x68598f0
2012-04-24 16:31:32.546 Know[7278:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString getDate]: unrecognized selector sent to instance 0x68598f0'
*** First throw call stack:
(0x13cb052 0x155cd0a 0x13ccced 0x1331f00 0x1331ce2 0xa930 0xb85c9 0xb8480 0x1fc849 0x1ff722 0xaf7c7 0xaf2c1 0xb228c 0xb6783 0x61301 0x13cce72 0x1d7592d 0x1d7f827 0x1d05fa7 0x1d07ea6 0x1d9330c 0x33530 0x139f9ce 0x1336670 0x13024f6 0x1301db4 0x1301ccb 0x12b4879 0x12b493e 0x22a9b 0x1d98 0x1cf5)
terminate called throwing an exception






在切换到使用NSKeyedArchiver / Unarchiver后,我还是遇到了同样的问题。这是我的新的检索和保存方法:


After I switched over to using NSKeyedArchiver/Unarchiver, I still ran into the same problem. Here are my new retrieve and save methods:

-(void)saveToUserDefaults:(NSMutableArray *)array{
    NSData *myEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:array];
    [[NSUserDefaults standardUserDefaults] setObject:myEncodedObject forKey:@"rawArray"];
}

-(NSMutableArray *)loadFromUserDefaults{
    NSData *myDecodedObject = [[NSUserDefaults standardUserDefaults] objectForKey:@"rawArray"];
    NSMutableArray *decodedArray =[NSKeyedUnarchiver unarchiveObjectWithData: myDecodedObject];
    return decodedArray;
}

这是程序崩溃的新行代码:

Here are the new lines of code that crash the program:

Entry *currEntry = [[self loadFromUserDefaults] objectAtIndex:section];
NSLog(@"date: %@", [currEntry getDate]);

这是更新的崩溃日志:

2012-04-24 17:43:36.853 Know[10314:f803] -[__NSCFString getDate]: unrecognized selector sent to instance 0x6acc260
2012-04-24 17:43:36.904 Know[10314:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString getDate]: unrecognized selector sent to instance 0x6acc260'
*** First throw call stack:
(0x13ce052 0x155fd0a 0x13cfced 0x1334f00 0x1334ce2 0xb710 0xbb5c9 0xbb480 0x1ff849 0x202722 0xb27c7 0xb22c1 0xb528c 0xb9783 0x64301 0x13cfe72 0x1d7892d 0x1d82827 0x1d08fa7 0x1d0aea6 0x1d9630c 0x36530 0x13a29ce 0x1339670 0x13054f6 0x1304db4 0x1304ccb 0x12b7879 0x12b793e 0x25a9b 0x2a18 0x2975)
terminate called throwing an exception


推荐答案

我认为问题是你存储一个自定义数组对象通过NSUserDefaults。也许你应该检查这篇文章:如何在NSUserDefaults中保存自定义类数组或此问题

I think that the proble is that you are storing an array of custom objects via NSUserDefaults. Maybe you should check this article about this: How to Save array of custom class in NSUserDefaults or this question.

EDIT

是否添加了 encodeWithCoder initWithCoder 方法。也许问题在于它的实现。

Did you add the methods encodeWithCoder and initWithCoder. Maybe the problem is in its implementation.

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

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