嗯,我不知道我的标题是否草拟得很好,但我会尝试解释我的问题是什么,我想在NSUserDefaults中为一个IndexPath保存一个NSDate,这种情况发生在viewWillDisappear
但它崩溃了,它正确保存因为当我重新打开DatePicker时会加载我想要的日期,但在UserDefaults中保存日期时仍会崩溃
所以继承我的代码,以便你可以看到最新情况......
我读过NSUserDefaults是否为零所以我可以加载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];
}
当我想在发生崩溃时将日期保存在viewWillDisappear
中:
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];
}
但成功保存的信息和日期选择器会重新启动日期。
崩溃日志:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]'
所以希望我解释得很好,谢谢!
设置索引路径:
- (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"];
}
答案 0 :(得分:0)
听起来您想保存选定的索引路径和日期。在NSUserDefaults中保存日期很简单。
// 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];
稍后以这种方式取回:
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
}
存储索引路径有点棘手,但这可行:
// 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]];
关键是这个答案没有:没有NSData,没有NSKeyedArchiver,没有字符串操作来构建索引路径表示。祝你好运。