0

So I'm getting an issue with NSUserDefaults when saving an array. 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.

- (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.

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. 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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • http://stackoverflow.com/questions/10305659/not-sure-how-to-handle-terminating-app-due-to-uncaught-exception-nsinvalidargu – Obj-Swift Mar 26 '14 at 19:59

4 Answers4

0

NSUserDefaults only supports plist objects so you can't save custom objects into it. You're adding an object of type AG_Storage into your array and saving it, which isn't allowed. You'll want to look at NSKeyedArchiving instead by conforming AG_Storage to the NSCoding protocol. Let me know if you need more guidance on that.

Jai Govindani
  • 3,181
  • 21
  • 26
0

Documentation says:

The NSUserDefaults class provides convenience methods for accessing common types such as floats, doubles, integers, Booleans, and URLs. 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

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. For example to implement NSCoding protocol.

Krivoblotsky
  • 1,492
  • 11
  • 17
  • So should I make a local NSMutableArray with the contents of `mainarray`? Would that fix it or would I have to declare this in the class that all this info is coming from? – user3430084 Mar 26 '14 at 20:00
  • Nope, all of your NSMutableArray objects must be: NSData, NSString, NSNumber, NSDate instances. – Krivoblotsky Mar 26 '14 at 20:01
  • So how do I go about converting them? I'm sorry I'm pretty new to some of these concepts. I get that it can't be stored because its an AG_Storage instance, but how do I set the array so I can store it correctly? Is there another way to save data that would be easier than this? I'm going to need to delete this info too – user3430084 Mar 26 '14 at 21:08
0

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

Your AG_Storage isn't one of the default NSCoding-compliant classes.

Community
  • 1
  • 1
Cameron
  • 1,142
  • 8
  • 32
0

In case someone comes up with this problem, make sure there's no null/nil in the items you're saving, this will throw an exception if you're trying to save a null object.

Waclock
  • 1,686
  • 19
  • 37