gpt4 book ai didi

iphone - 写入 plist 字典中的字符串

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:49:12 26 4
gpt4 key购买 nike

我使用的 plist 结构如下:plist, array, dict key string key string/dict, dict key string key string/dict,/array,/plist。

我希望通过以下代码将当前字典中“Done”字符串的值设置为“Yes”。

但是现在,代码用当前字典的字符串替换了整个字典数组(尽管“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];
}

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

最佳答案

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

此外,您可以使用[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] initWithArray:objectsArray];

确保在使用保留的属性 setter 时自动释放。

关于iphone - 写入 plist 字典中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11923593/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com