在plist字典中写入字符串
我正在使用这样结构的plist:plist,array,dict key string key string / dict,dict key string key string / dict,/ array,/ plist。
我想用下面的代码将当前字典中的“Done”字符串值设置为“Yes”。
但是现在,代码用当前字典的字符串替换了整个字典数组 (尽管如此,“Done”键的值为“是”。)
什么是我想要的正确代码?
在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部分:
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]; }
另外,代替@"Yes"
,您可以使用[NSNumber numberWithBool:YES]
:
[mutDict setObject:[NSNumber numberWithBool:YES] forKey:@"Done"];
通常会更好的forms然后使用@"Yes"
。
和:
districtLabel.text = [[detailsDataSource objectAtIndex: detailIndex] valueForKey:@"Yes"];
应该:
districtLabel.text = [[detailsDataSource objectAtIndex: detailIndex] objectForKey:@"Yes"];
还注意到您可能有内存泄漏:
detailViewController.detailsDataSource = [[NSArray alloc] initWithArray:objectsArray];
使用保留的属性设置器时,请确保自动释放。
[[detailsDataSource objectAtIndex: detailIndex] setObject:@"Yes" forKey:@"Done"];