我使用的 plist 结构如下:plist, array, dict key string key string/dict, dict key string key string/dict,/array,/plist.
我想要使用以下代码将当前字典中的“完成”字符串的值设置为"is"。
但是现在,代码用当前字典的字符串替换了整个字典数组(不过,“完成”键的值应该是"is"。)
什么是我想要的正确代码?
在 DetailViewController.m 中:
-(IBAction)favoriteButtonPressedid)sender
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent"Object.plist"];
[[detailsDataSource objectAtIndex: detailIndex] setValue"Yes" forKey"Done"];
[[detailsDataSource objectAtIndex: detailIndex] writeToFile:path atomically:YES];
}
detailsDataSource 和 detailIndex 是从 TableViewController.m segue 收到的。
TableViewController.m segue 部分:
detailViewController.detailsDataSource = [[NSArray alloc]initWithArraybjectsArray];
detailViewController.detailIndex = selectedRowIndex.row;
DetailViewController viewDidLoad
if ([[[detailsDataSource objectAtIndex: detailIndex] valueForKey"Favorite"] isEqual"Yes"]) {
[favoriteButton setImage:[UIImage imageNamed"favoritedItem.png"] forState:UIControlStateSelected | UIControlStateHighlighted];
}
districtLabel.text = [[detailsDataSource objectAtIndex: detailIndex] valueForKey"District"];
detailsDataSource 数组在 detailViewController 中像这样使用,所以我不能从数组更改为字典,在这种情况下必须制作一个可变副本。
Best Answer-推荐答案 strong>
setValue:forKey 不用于修改可变字典。您应该使用 setObject:forKey 。即,这个:
[[detailsDataSource objectAtIndex: detailIndex] setValue"Yes" forKey"Done"];
应该是:
[[detailsDataSource objectAtIndex: detailIndex] setObject"Yes" forKey:@"Done"];
如果这不能解决问题,请确保字典和数组都是可变的:
-(IBAction)favoriteButtonPressedid)sender {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Object.plist"];
NSMutableDictionary *mutDict = [NSMutableDictionary dictionaryWithDictionary:[detailsDataSource objectAtIndex:detailIndex]];
[mutDict setObject:@"Yes" forKey:@"Done"];
NSMutableArray *tmpMutArr = [NSMutableArray arrayWithArray:detailsDataSource];
[tmpMutArr replaceObjectAtIndex:detailIndex withObject:[NSDictionary dictionaryWithDictionary:mutDict]]; // make dict immutable before replacing. Casting it ( (NSDictionary *)mutDict ) works too.
[detailsDataSource release], detailsDataSource = nil;
detailsDataSource = [[NSArray alloc] initWithArray:tmpMutArr];
[detailsDataSource writeToFile:path atomically:YES];
}
另外,您可以使用 [NSNumber numberWithBool:YES] 代替 @"Yes" :
[mutDict setObject:[NSNumber numberWithBool:YES] forKey:@"Done"];
通常会比使用 @"Yes" 更好。
还有:
districtLabel.text = [[detailsDataSource objectAtIndex: detailIndex] valueForKey:@"Yes"];
应该是:
districtLabel.text = [[detailsDataSource objectAtIndex: detailIndex] objectForKey:@"Yes"];
还注意到您可能存在内存泄漏:
detailViewController.detailsDataSource = [[NSArray alloc] initWithArraybjectsArray];
确保在使用保留的属性 setter 时自动释放。
关于iphone - 写入 plist 字典中的字符串,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/11923593/
|