Plist NSMutableArray & NSMutableDictionary Issue
i am having trouble trying to work the plist in my app. i have created a custom one and it is being copied to the documents folder so that the values can be added. i have so far setup everything and everything is appearing fine in it how ever when i edit a value and save it开发者_运维知识库 all the values seem to change to what ever the one was that i just saved. i am going a little crazy over this problem as it seams like i fix it but it keeps failing. Also my plist format is Root - Array Item 0 - Dictionary Setting - String Value - String Item 1 - Dictionary Setting - String Value - String Item 1 - Dictionary Setting - String Value - String OtherValue - String
Variables for Rootfile NSMutableArray *settingsArray;
App is in a tableview and has a detail view with one text box.
-(void)viewdidload{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"settings.plist"];
NSMutableArray *tmpArray = [[NSMutableArray alloc] initWithContentsOfFile:path];
self.settingsArray = tmpArray;
[tmpArray release];
}
inside the cellForRowAtIndexPath method
// Configure the cell.
cell.textLabel.text = [[self.settingsArray objectAtIndex:indexPath.row] objectForKey:@"Setting"];
return cell;
inside the didselectrowatindex method
drinkDetailViewController.settingsDictionary = [self.settingsArray objectAtIndex:indexPath.row];
Detailview variables
NSMutableDictionary *settingsDictionary;
NSMutableArray *settingValArray;
in the detailview viewwillappear method
settingNamelabel.text = [settingsDictionary objectForKey:@"Setting"];
valueTextField.text = [settingsDictionary objectForKey:@"Value"];
This is the save method
- (IBAction) save: (id) sender {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"settings.plist"];
NSMutableArray *tmpArray = [[NSMutableArray alloc] initWithContentsOfFile:path];
self.settingValArray = tmpArray;
[settingValArray setValue:valueTextField.text forKey:"Value"];
[settingValArray writeToFile:path atomically:YES];
}
The problem is that in your save method, you are setting the value for "Value" against the entire array of dictionaries, and not for an individual dictionary.
If you keep the index of the specific dictionary around, then you could do something like:
[[settingValArray objectAtIndex:dictionaryIndex] setValue:valueTextField.text forKey:"Value"];
[settingValArray writeToFile:path atomically:YES];
精彩评论