我有一个基于用户输入的其他数组的计算(使用不同公式)的结果数组。我想在用户单击“保存结果”按钮时保存数组。另外,如果他再次进入数组,下一个结果数组应该单独保存,以便有一个结果数组的可变数组。
我的应用程序中有三个表格 View
- 通过文本字段的用户条目表
- 结果标题(所有之前和最后的结果)
- 详细结果表(例如特定日期的结果)
我为此使用了 NSUserdefaults 并且保存成功,但是当我再次输入条目数组时,结果数组将覆盖以前的结果数组 我想知道如何做到这一点(仅通过 NSUserdefaults)。
有人可以通过具体的代码示例来阐明执行此操作的“正确”方法吗? 谢谢
好的,这是我的代码片段 在 View 中确实加载了
resultsDict = [[NSMutableDictionary alloc] init];
表格 View 方法。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSArray *array = [results objectAtIndex:section];
return [array count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [results count ];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell=nil;
if (cell == nil) {
cell = [[[MedicalAirSizingResultsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
//switch for calculating the result values for current section
switch ( indexPath.section) {
case 0:
//switch to get the current result value
switch (indexPath.row) {
case 0:
resultValue = [self Altitude_above_sea_level];
break;
case 1:
resultValue = [self Intake_air_temperature];
break;
case 2:
resultValue = [self Relative_Humidity];
break;
}
break;
case 1:
switch (indexPath.row) {
case 0:
resultValue = [self System_Model];
break;
case 1:
resultValue = [self Horsepower];
break;
case 2:
resultValue = [self System_Capacity];
break; ....and so on
case 8:
resultValue = [self R_Suggd_Specs];
break;
}
break;
}
//nsdictionary to add in nsuserdefaults
[vaccumResultsDict setObject:[NSNumber numberWithInt:resultValue] forKey:[NSString stringWithFormat:@"%d%d",indexPath.section, indexPath.row]];
NSArray *array = [results objectAtIndex:indexPath.section];
if ([indexPath section]== 3 && [indexPath row] == [array count]) {
UIButton *btnShowResults = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnShowResults.opaque=YES;
[btnShowResults setBackgroundColor:[UIColor clearColor]];
[btnShowResults setBackgroundImage:[UIImage imageNamed:@"ShowResults.png"] forState:UIControlStateNormal];
[btnShowResults setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
btnShowResults.frame = CGRectMake(0, 0, 320,40);
[btnShowResults addTarget:self action:@selector(showVaccumSizingSavedResults)forControlEvents:UIControlEventTouchDown];
[cell addSubview:btnShowResults];
}
else{
NSString *str = [array objectAtIndex:indexPath.row];
[(VaccumSizingResultsCell *)cell setData:str andResult:resultValue];
}
return cell;
}
//当用户点击最后一节最后一行的保存结果按钮时将调用此方法 -(void)showVaccumSizingSavedResults{
NSUserDefaults *savedVaccumResultDefaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *savedResultsArray = [[NSMutableArray alloc]init];
[savedResultsArray addObject:vaccumResultsDict];
[vaccumResultsDict release];
[savedVaccumResultDefaults setObject:savedResultsArray forKey:@"savedVaccumSizingResultsKey"];
[savedVaccumResultDefaults synchronize];
GetResult *saveCurrentResult = [[GetResult alloc]initWithNibName:@"GetResult" bundle:nil];
[self.navigationController pushViewController:saveCurrentResult animated:YES];
}
最佳答案
如果要保存的数组相当小,并且仅包含基本数据类型(NSData、NSDate、NSNumber、NSString、NSArray 或 NSDictionary 的实例),您只需使用 writeToFile:atomically:
将它们写入文件即可. NSUserDefaults
不是存储此类数据的地方。如果您的数据庞大且复杂,CoreData 是一个不错的选择。
关于objective-c - 在iOS编程中保存/检索数组的最佳方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7999284/