我有一系列词典,它保存在app的文档目录中 如何更新特定键和索引的值? 例如:我想更新第二个词典人名
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>personName</key>
<string>John Sculey</string>
<key>isPersonAdult</key>
<true/>
<key>personID</key>
<integer>12541</integer>
<key>personPicture</key>
<string>john.jpg</string>
<key>personDesignation</key>
<string>Branding Manager</string>
<key>personDepartment</key>
<integer>2</integer>
<key>personEmail</key>
<string>john@johnsculey.com</string>
</dict>
<dict>
<key>personName</key>
<string>Chris Seattle</string>
<key>isPersonAdult</key>
<true/>
<key>personID</key>
<integer>89854</integer>
<key>personPicture</key>
<string>chris.jpg</string>
<key>personDesignation</key>
<string>Branding Manager</string>
<key>personDepartment</key>
<integer>2</integer>
<key>personEmail</key>
<string>chris@johnsculey.com</string>
</dict>
依旧......
答案 0 :(得分:1)
所以,假设你的字典是可变的,
[[array objectAtIndex:1] setObject:@"New Name" forKey:@"personName"];
(如果不是,请在之前创建一个可变副本。)
答案 1 :(得分:0)
不确定,如果我的问题是正确的。但是如果你问,如何在数组的特定索引处访问字典,这将是这样的:
NSMutableDictionary *person = [NSMutableDictionary dictionaryWithDictionary:[array objectAtIndex:1]];
[person setValue:@"Chris Washington" forKey:@"personName"];
没关系,数组包含什么样的对象;它可以是数字或 - 在您的情况下 - 字典对象。您可以正常访问它们。
答案 2 :(得分:0)
我的2美分:D
id data = [NSData dataWithContentsOfFile:plistFile];
NSArray *plist = [NSPropertyListSerialization propertyListWithData:data options:NSPropertyListMutableContainersAndLeaves format:nil error:nil];
assert([plist isKindOfClass:[NSArray class]]);
//every element in the array is mutable
NSMutableDictionary *dict = plist[1];
dict[@"key"] = @"value";
答案 3 :(得分:-1)
您可以使用几行代码执行所需操作...首先,您从阵列中获取要编辑的词典,然后更改密钥的值。我没有测试过这段代码,但它应该运行良好!
NSURL *documents = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
NSURL *plistUrl = [documents URLByAppendingPathComponent:@"yourPlistName.plist"];
NSArray *plistContents = [NSArray arrayWithContentsOfURL:plistUrl];
NSMutableDictionary *personToEdit = [plistContents objectAtIndex:1];
[personToEdit setValue:@"value you want to set" forKey:@"your key"];