Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the best way to save/retrieve array in iOS Programming
    primarykey
    data
    text
    <p>i have an array of results based on calculation(with different formulae) of other array input by the user. i want to save the array as user clicks on "save results" button. Also if he again enters array, the next results array should be saved separately so that there be a mutable array of result arrays. </p> <p>There are three table views in my app</p> <ol> <li>User entries table through text fields</li> <li>Results titles( All Previous and the last results) </li> <li>Detail results table (eg results on a particular day)</li> </ol> <p>i've used NSUserdefaults for this and it saved successfully but when i again enters the entries array then results array override the previous results array i want to know what is the way to do this( through NSUserdefaults only).</p> <p>Could anybody clarify the "right" way to do this with a concrete code example? Thanks</p> <p>ok here is piece of my code in view did load</p> <pre><code>resultsDict = [[NSMutableDictionary alloc] init]; </code></pre> <p>table view methods.</p> <pre><code>- (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 &amp;&amp; [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; } </code></pre> <p>//this method will be called when user taps on save resutls button which is in last row of last section -(void)showVaccumSizingSavedResults{</p> <pre><code> 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]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload