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.
There are three table views in my app
- User entries table through text fields
- Results titles( All Previous and the last results)
- Detail results table (eg results on a particular day)
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).
Could anybody clarify the “right” way to do this with a concrete code example?
Thanks
ok here is piece of my code
in view did load
resultsDict = [[NSMutableDictionary alloc] init];
table view methods.
- (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;
}
//this method will be called when user taps on save resutls button which is in last row of last section
-(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];
}
You’re asking way to much of
NSUserDefaults. I would recommend using Core Data for saving this data. Core Data is fairly easy to use and learn, but much to big of a topic to cover with a “concrete code example” in a this answer. You could start with Apple’s documentation or with one of the many Core Data books.