Reputation: 2668
Well I dont know if my title is well drafted but I will try to explain whats my problem, I want to save a NSDate for an IndexPath in NSUserDefaults, this happens when viewWillDisappear
but its crashing, its saving correctly because when I reopen the DatePicker loads the date I want but still crash when saving a date at the UserDefaults
So heres my code so you can see whats going on....
I read if the NSUserDefaults is nil or not so I can load the DatePicker:
NSArray *indexParams = [self.userdefaults objectForKey:@"indexpath"];
NSIndexPath *myIndexPath = [NSIndexPath indexPathForRow:indexParams[1]
inSection:indexParams[0]];
self.notificationDate = [self.userdefaults objectForKey:@"date"];
NSDate *date = [self.userdefaults objectForKey:[NSString stringWithFormat:@"%d", myIndexPath.row]];
if(date){
[self.NotSwith setOn:YES];
self.DatePicker.date = [self.userdefaults objectForKey:[NSString stringWithFormat:@"%d", myIndexPath.row]];
}else{
[self.NotSwith setOn:NO];
}
When I want to save the date in viewWillDisappear
its when the crash happens:
NSArray *indexParams = [self.userdefaults objectForKey:@"indexpath"];
NSIndexPath *myIndexPath = [NSIndexPath indexPathForRow:indexParams[1]
inSection:indexParams[0]];
NSDate *date = [self.userdefaults objectForKey:[NSString stringWithFormat:@"%d", myIndexPath.row]];
if(date){
// [self.userdefaults synchronize];
}
else{
[self.userdefaults setObject:self.DatePicker.date forKey:[NSString stringWithFormat:@"%d", myIndexPath.row]];
[self.userdefaults synchronize];
[[UIApplication sharedApplication] scheduleLocalNotification:local];
}
But the info its saved successfully and the date picker loads the date in relaunch.
crash log:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]'
So hope I explained well, Thanks!
Setting the indexpath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSNumber *section = [NSNumber numberWithInt:indexPath.section];
NSNumber *rows = [NSNumber numberWithInt:indexPath.row];
[self.userdefaults setObject:@[section, rows] forKey:@"indexpath"];
}
Upvotes: 1
Views: 471
Reputation: 62686
Sounds like you want to save both the selected index path and a date. Saving a date in NSUserDefaults is easy.
// no need to keep a property on self for user defaults. you don't need to keep
// that around. just a stack variable will work.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDate *date = [NSDate date];
[defaults setObject:date forKey:@"myDate"];
[defaults synchronize];
Get it back later on this way:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDate *date = [defaults objectForKey:@"myDate"];
// if nothing has been stored using that key then objectForKey will return nil
if (date) {
// it's there!
} else {
// it's not there
}
Storing an index path is a little more tricky, but this will work:
// wrap your row and section in NSNumbers, wrap those in an array for brevity
NSNumber *section = [NSNumber numberWithInt:myIndexPath.section];
NSNumber *row = [NSNumber numberWithInt:myIndexPath.row];
[defaults setObject:@[section, row] forKey:@"myIndexPath"]; // then synchronize
// naturally, when you get it later, you can check for nil again, and,
// if it's not nil, to rebuild the index path...
NSArray *indexParams = [defaults objectForKey:@"myIndexPath"];
NSIndexPath *myIndexPath = [NSIndexPath indexPathForRow:indexParams[1]
inSection:indexParams[0]];
The key is what isn't in this answer: no NSData, no NSKeyedArchiver, no string manipulation to build an index path representation. Best of luck.
Upvotes: 2