我正在使用一个像这样的结构的plist:plist,array,dict key string key string / dict,dict key string key string / dict,/ array,/ plist。
使用以下代码我想要将当前词典中的“完成”字符串值设置为“是”。
但是现在,代码用当前字典的字符串替换了整个字典数组(尽管如此,“Done”键值为“Yes”。)
我想要的正确代码是什么?
DetailViewController.m中的:
-(IBAction)favoriteButtonPressed:(id)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];
}
如果重要的话,可以从TableViewController.m segue中获取detailsDataSource和detailIndex。
TableViewController.m segue part:
detailViewController.detailsDataSource = [[NSArray alloc]initWithArray:objectsArray];
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中使用如下所示我无法从数组更改为字典,在这种情况下必须创建一个可变副本。
答案 0 :(得分:2)
setValue:forKey
不是用于修改可变字典。您应该使用setObject:forKey
。即,这个:
[[detailsDataSource objectAtIndex: detailIndex] setValue:@"Yes" forKey:@"Done"];
应该是:
[[detailsDataSource objectAtIndex: detailIndex] setObject:@"Yes" forKey:@"Done"];
如果这不能解决问题,请确保字典和数组都是可变的:
-(IBAction)favoriteButtonPressed:(id)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];
}
此外,您可以使用@"Yes"
代替[NSNumber numberWithBool: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] initWithArray:objectsArray];
还注意到可能内存泄漏:
{{1}}
确保在使用保留的属性设置器时自动释放。
答案 1 :(得分:2)
[[detailsDataSource objectAtIndex: detailIndex] setObject:@"Yes" forKey:@"Done"];