Plist NSMutableArray 和 Plist NSMutableDictionary 问题
我在尝试在我的应用程序中使用 plist 时遇到问题。我创建了一个自定义文件夹,并将其复制到文档文件夹中,以便可以添加值。到目前为止,我已经设置了所有内容,并且一切看起来都很好,但是当我编辑一个值并保存它时,所有值似乎都更改为我刚刚保存的值。我对这个问题有点疯狂,因为它看起来像是我解决了它,但它总是失败。我的 plist 格式也是 根 - 数组 项目 0 - 字典 设置 - 字符串 值 - 字符串 第 1 项 - 字典 设置 - 字符串 值 - 字符串 第 1 项 - 字典 设置 - 字符串 值 - 字符串 OtherValue - 根文件的字符串
变量 NSMutableArray *settingsArray;
应用程序位于表格视图中,并具有带有一个文本框的详细视图。
-(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];
}
cellForRowAtIndexPath方法里面
// Configure the cell.
cell.textLabel.text = [[self.settingsArray objectAtIndex:indexPath.row] objectForKey:@"Setting"];
return cell;
didselectrowatindex方法里面
drinkDetailViewController.settingsDictionary = [self.settingsArray objectAtIndex:indexPath.row];
Detailview变量
NSMutableDictionary *settingsDictionary;
NSMutableArray *settingValArray;
在detailview viewwillappear方法里面
settingNamelabel.text = [settingsDictionary objectForKey:@"Setting"];
valueTextField.text = [settingsDictionary objectForKey:@"Value"];
这是save方法
- (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];
}
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];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于,在您的保存方法中,您是针对整个字典数组而不是单个字典设置“Value”的值。
如果您保留特定字典的索引,那么您可以执行以下操作:
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: