好吧,我不知道我的标题是否起草得好,但我会尝试解释我的问题是什么,我想在 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"];
}
最佳答案
听起来您想同时保存选定的索引路径和日期。在 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,没有用于构建索引路径表示的字符串操作。祝你好运。
关于ios - 如何查看 NSUserdefaults 是否为零?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22647278/