2

我正在使用具有如下结构的 plist:plist、array、dict key string key string /dict、dict key string key string /dict、/array、/plist。

我想要使​​用以下代码将当前字典中的“完成”字符串的值设置为“是”。

但是现在,代码用当前字典的字符串替换了整个字典数组(不过,“完成”键的值应该是“是”。)

我想要的正确代码是什么?

在 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];
}

如果重要, detailsDataSource 和 detailIndex 是从 TableViewController.m segue 收到的。

TableViewController.m segue 部分:

    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 中像这样使用,所以我不能从数组更改为字典,在这种情况下必须制作一个可变副本。

4

2 回答 2

2
[[detailsDataSource objectAtIndex: detailIndex] setObject:@"Yes" forKey:@"Done"];
于 2012-08-12T16:17:50.173 回答
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];

确保在使用保留的属性设置器时自动释放。

于 2012-08-12T16:15:30.257 回答