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.