我正在编写一个需要保存和恢复nsmutablearrays的应用程序来填充表格视图。数组将保存,但一旦重置所有内容将不会覆盖。我发现这些值组合在一起并且给了我不可靠的平均值...代码如下。我也有各种具有此圈功能的视图控制器......
// code that adds items to table view and saves a version of the array to nsmutablearray
NSUserDefaults *prefs = [[NSUserDefaults alloc]
initWithSuiteName:@"group.com.DJ.TSTML"];
[tableItems3 insertObject:TimeString2 atIndex:0];
// time string is the time as a nsstring
[tableItems4 addObject:[NSNumber numberWithFloat:seconds2]];
[tableview reloadData];
[prefs setObject:self.tableItems4 forKey:@"SavedArray3"];
[prefs setObject:self.tableItems3 forKey:@"SavedArray4"];
// this is saving to insurer defaults
NSString *newString = [NSString stringWithFormat:@"%lu", (unsigned long)tableItems3.count];
[Commentcount setText:newString];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
NSNumber *average = [tableItems4 valueForKeyPath:@"@avg.self"];
Average.text = [formatter stringFromNumber:average];
NSLog(@"Sucess");
[prefs synchronize];
} }
恢复阵列:
NSUserDefaults *prefs = [[NSUserDefaults alloc]
initWithSuiteName:@"group.com.DJ.TSTML"];
// code used to erase current values in the table view and restore from NSUserDefaults
tableItems3 = [[NSMutableArray alloc] init];
tableItems4 = [[NSMutableArray alloc] init];
[tableview reloadData];
NSMutableArray *RestoreArray1 = [[prefs objectForKey:@"SavedArray4"]mutableCopy];
NSMutableArray *RestoreArray2 = [[prefs objectForKey:@"SavedArray3"]mutableCopy];
[tableItems3 addObjectsFromArray:RestoreArray1];
[tableItems4 addObjectsFromArray:RestoreArray2];
if (tableItems3 == nil) {
tableItems3 = [[NSMutableArray alloc] init];
tableItems4 = [[NSMutableArray alloc] init];
}
[tableview reloadData];
NSLog(@"%@",tableItems4);
}
}
提前感谢您的帮助! 我的问题要明确的是,每次我保存到nsuserdefaults而不是覆盖前一个数组时,它只保留旧数组并与新数组结合
答案 0 :(得分:0)
好的,我回答了我自己的问题。有人提出它是因为我正在检索而不首先覆盖它。以下代码是有用的......
NSString *Clearstring = [prefs objectForKey:@"Clear?"];
// i created a nsstring that when the method for restarting would change accordingly.
// so when someone would navigate to this view controller this would fire.
// if clear is yes i would prevent the code that controled the restore function from restoring and prepare
// it if it was needed to be restored again.
if ([Clearstring isEqualToString:@"Yes"]) {
tableItems3 = [[NSMutableArray alloc] init];
tableItems4 = [[NSMutableArray alloc] init];
[prefs setObject:self.tableItems4 forKey:@"SavedArray3"];
[prefs setObject:self.tableItems3 forKey:@"SavedArray4"];
[prefs setObject:@"NO" forKey:@"Clear?"];
[tableview reloadData];
}
else{
tableItems3 = [[NSMutableArray alloc] init];
tableItems4 = [[NSMutableArray alloc] init];
[tableview reloadData];
NSMutableArray *RestoreArray1 = [[prefs objectForKey:@"SavedArray4"]mutableCopy];
NSMutableArray *RestoreArray2 = [[prefs objectForKey:@"SavedArray3"]mutableCopy];
[tableItems3 removeAllObjects];
[tableItems4 removeAllObjects];
[tableItems3 addObjectsFromArray:RestoreArray1];
[tableItems4 addObjectsFromArray:RestoreArray2];
if (tableItems3 == nil) {
tableItems3 = [[NSMutableArray alloc] init];
tableItems4 = [[NSMutableArray alloc] init];
}
[tableview reloadData];
NSLog(@"%@",tableItems4);
}
}