我有一個基於用戶輸入的其他數組的計算結果(使用不同的公式)的結果數組。我想保存數組,當用戶點擊「保存結果」按鈕。另外,如果他再次輸入數組,則應單獨保存下一個結果數組,以便有一個可變結果數組數組。在iOS編程中保存/檢索數組的最佳方式是什麼?
有我的應用程序三個表視圖
- 用戶通過在特定的一天文本字段
- 結果標題(在此之前,最後的結果)
- 詳細成績表(如結果條目表)
我已經使用NSUserdefaults爲此,它保存成功,但是當我再次進入條目數組然後結果數組覆蓋以前的結果數組 我想知道如何做到這一點(僅通過NSUserdefaults)。
任何人都可以澄清「正確」的方式來做到這一點與具體的代碼示例? 感謝
確定這裏是一張我的視圖代碼 做負載
resultsDict = [[NSMutableDictionary alloc] init];
表視圖的方法。
- (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;
}
//這個方法會被當用戶點擊保存按鈕resutls這是最後一節 的最後一排叫 - (無效){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];
}
請向我們顯示您已擁有的代碼。 – PengOne