在iOS编程中保存/检索数组的最佳方法是什么

时间:2011-11-03 17:29:36

标签: objective-c ios arrays nsuserdefaults

我有一个基于用户输入的其他数组的计算(使用不同公式)的结果数组。我想在用户点击“保存结果”按钮时保存数组。此外,如果他再次进入数组,则应单独保存下一个结果数组,以便有一个可变数组的结果数组。

我的应用中有三个表格视图

  1. 用户条目表通过文本字段
  2. 结果标题(所有上一个和最后一个结果)
  3. 详细结果表(例如特定日期的结果)
  4. 我已经使用了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;
    }
    

    //当用户点击最后一节最后一行的“保存结果”按钮时,将调用此方法      - (无效)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];
    }
    

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

你要问NSUserDefaults的大部分内容。我建议使用Core Data来保存这些数据。核心数据相当容易使用和学习,但在本答案中用“具体的代码示例”来涵盖很大的主题。您可以从Apple's documentation开始,也可以使用众多核心数据书籍之一。

相关问题