2011-11-03 57 views
0

我有一个基于用户输入的其他数组的计算结果(使用不同的公式)的结果数组。我想保存数组,当用户点击“保存结果”按钮。另外,如果他再次输入数组,则应单独保存下一个结果数组,以便有一个可变结果数组数组。在iOS编程中保存/检索数组的最佳方式是什么?

有我的应用程序三个表视图

  1. 用户通过在特定的一天文本字段
  2. 结果标题(在此之前,最后的结果)
  3. 详细成绩表(如结果条目表)

我已经使用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]; 
} 
+1

请向我们显示您已拥有的代码。 – PengOne

回答

0

你问办法不多NSUserDefaults。我建议使用Core Data来保存这些数据。核心数据相当容易使用和学习,但对于这个答案中的一个“具体代码示例”所涵盖的主题来说很重要。您可以从Apple's documentation开始,也可以从许多Core Data书籍中选择一本开始。

+0

谢谢.....实际上我只是想知道如何保留结果数组的* previous value *。 –

+0

您必须阅读以前保存的结果并将其添加到savedResultsArray中,以构建更大的结果数组。您的代码现在创建一个空数组并仅添加最新结果。当你超出NSUserDefaults的限制来尝试保存那么多的对象时,你会想重新考虑我的答案。 –

1

如果要保存的数组非常小,并且只包含基本数据类型(NSData,NSDate,NSNumber,NSString,NSArray或NSDictionary的实例),您可以使用writeToFile:atomically:将它们写入文件。 NSUserDefaults不是存储这种数据的地方。如果您的数据庞大而复杂,CoreData是一个不错的选择。

相关问题