简体   繁体   English

由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,NSUserDefaults?

[英]Terminating app due to uncaught exception 'NSInvalidArgumentException', NSUserDefaults?

So I'm getting an issue with NSUserDefaults when saving an array. 所以我在保存数组时遇到了NSUserDefaults的问题。 All my code works up until this point, but as soon as I tried to save the arrays in NSUserDefaults I started having issues, mostly that now for some reason it doesn't save. 到目前为止,我所有的代码都可以正常工作,但是当我尝试将数组保存到NSUserDefaults中时,我开始遇到问题,主要是由于某种原因,它无法保存。

- (IBAction)unwindToList:(UIStoryboardSegue *)segue
 {

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MMMM dd, yyyy"];

AG_AddItemViewController *source = [segue sourceViewController];
AG_Storage *item = source.store;

NSDate *dateCreated = item.creationDate;



NSString *todayString = [dateFormat stringFromDate:self.todayDate];
NSString *dateCreatedString = [dateFormat stringFromDate:dateCreated];
NSString *tomorrowString = [dateFormat stringFromDate:self.tomorrowsDate];
NSString *yesterdayString = [dateFormat stringFromDate:self.yesterdaysDate];


//Set up file storage!

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];



if (item.itemName != nil) {

    if ([dateCreatedString isEqualToString:todayString]) {
        [self.mainArray addObject:item];
        [tableView reloadData];
        [defaults setObject:self.mainArray forKey:todayString];
        [defaults synchronize];

        NSLog(@"Saved");

    }
    else if ([dateCreatedString isEqualToString:tomorrowString]){
        [self.tomorrowArray addObject:item];
        [tableView reloadData];

        NSLog(@"THIS WORKED TOO :D");
    }
    else if ([dateCreatedString isEqualToString:yesterdayString]){
        [self.yesterdayArray addObject:item];
        [tableView reloadData];

        NSLog(@"THIS WORKED");
    }
    else{

    }
}
 }

mainarray and the other mutable arrays are all declared as properties in the interface, so they shouldn't be causing this issue. mainarray和其他可变数组都在接口中声明为属性,因此它们不应引起此问题。

2014-03-26 15:36:46.561 AgendaBk[32702:a0b] <AG_Storage: 0x8cc2c90>
     2014-03-26 15:36:46.563 AgendaBk[32702:a0b] Attempt to set a non-property-list object       (
     "<AG_Storage: 0x8cc2c90>"
     ) as an NSUserDefaults value for key March 26, 2014
     2014-03-26 15:36:46.565 AgendaBk[32702:a0b] *** Terminating app due to uncaught exception          'NSInvalidArgumentException', reason: '*** -[NSUserDefaults setObject:forKey:]: attempt to    insert non-property list object (
    "<AG_Storage: 0x8cc2c90>" ) for key March 26, 2014'

libc++abi.dylib: terminating with uncaught exception of type NSException

Note that this issue is only happening when I try to get the NSUserDefaults going. 请注意,仅当我尝试使NSUserDefaults运行时,才会发生此问题。 Everything else seems to be working fine, and when I added individual strings earlier it worked fine. 其他所有内容似乎都运行良好,并且当我较早添加单个字符串时,它运行良好。 I would really like to use this to store my arrays though if i can manage it. 我真的很想用它来存储我的数组,尽管我可以管理它。

NSUserDefaults only supports plist objects so you can't save custom objects into it. NSUserDefaults仅支持plist对象,因此您无法将自定义对象保存到其中。 You're adding an object of type AG_Storage into your array and saving it, which isn't allowed. 您正在将类型为AG_Storage的对象添加到数组中并保存它,这是不允许的。 You'll want to look at NSKeyedArchiving instead by conforming AG_Storage to the NSCoding protocol. 您将需要通过使AG_Storage符合NSCoding协议来查看NSKeyedArchiving Let me know if you need more guidance on that. 让我知道您是否需要更多指导。

Documentation says: 文档说:

The NSUserDefaults class provides convenience methods for accessing common types such as floats, doubles, integers, Booleans, and URLs. NSUserDefaults类提供了用于访问常见类型(例如浮点数,双精度数,整数,布尔值和URL)的便捷方法。 A default object must be a property list, that is, an instance of (or for collections a combination of instances of): NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary 默认对象必须是属性列表,即NSData,NSString,NSNumber,NSNumber,NSDate,NSArray或NSDictionary的一个实例(或用于集合的实例的组合)。

But you are trying to save AG_Storage instance.If you want to store any kind of objects you should try so serialize you object. 但是您正在尝试保存AG_Storage实例。如果要存储任何类型的对象,则应尝试序列化对象。 For example to implement NSCoding protocol. 例如实现NSCoding协议。

Take a look at this answer to a similar issue (I believe it's the same problem you are having): https://stackoverflow.com/a/19720674/694080 看看这个问题的答案(我相信这是您遇到的相同问题): https : //stackoverflow.com/a/19720674/694080

Your AG_Storage isn't one of the default NSCoding-compliant classes. 您的AG_Storage不是与NSCoding兼容的默认类之一。

万一有人提出这个问题,请确保要保存的项目中没有null / nil ,如果您要保存null对象,这将引发异常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “因未捕获的异常而终止应用程序&#39;NSInvalidArgumentException&#39;” - “Terminating app due to uncaught exception 'NSInvalidArgumentException'” 由于未捕获的异常&#39;NSInvalidArgumentException 4而终止应用程序 - Terminating app due to uncaught exception 'NSInvalidArgumentException 4 ***由于未捕获的异常&#39;NSInvalidArgumentException&#39;而终止应用, - *** Terminating app due to uncaught exception 'NSInvalidArgumentException', 由于未捕获而终止应用程序 &gt; 异常“NSInvalidArgumentException” - Terminating app due to uncaught > exception 'NSInvalidArgumentException' 由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序, - Terminating app due to uncaught exception 'NSInvalidArgumentException', 由于未捕获的异常NSInvalidArgumentException而终止应用程序 - Terminating app due to uncaught exception NSInvalidArgumentException 由于SDWEbImage中未捕获的异常&#39;NSInvalidArgumentException&#39;而终止应用 - Terminating app due to uncaught exception 'NSInvalidArgumentException' in SDWEbImage 是否由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序? - Terminating app due to uncaught exception 'NSInvalidArgumentException'? 由于未捕获的异常&#39;NSInvalidArgumentException&#39;错误而终止应用程序 - Terminating app due to uncaught exception 'NSInvalidArgumentException' error iOS - 由于未捕获的异常&#39;NSInvalidArgumentException&#39;而终止应用程序 - iOS - Terminating app due to uncaught exception 'NSInvalidArgumentException'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM