本文介绍了在plist字典中写入字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个像这样的结构的plist:plist,array,dict key string key string / dict,dict key string key string / dict,/ array,/ plist。



我希望用以下代码将将当前字典中的Done字符串值设置为是。



但现在,代码用当前字典的字符串替换整个字典数组(尽管如此,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:@YesforKey:@Done];
[[detailsDataSource objectAtIndex:detailIndex] writeToFile:path atomically:YES];
}

如果重要的话,可以从TableViewController.m segue中获取detailsDataSource和detailIndex。 / p>

TableViewController.m segue part:

  detailViewController.detailsDataSource = [[ NSArray alloc] initWithArray:objectsArray]; 
detailViewController.detailIndex = selectedRowIndex.row;

DetailViewController viewDidLoad

  if([[[dataDataSource 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:@YesforKey:@Done]; 

应该是:

  [[detailsDataSource objectAtIndex:detailIndex] setObject:@YesforKey:@Done]; 

如果这不能解决问题,请确保字典和数组都是可变的:

   - (IBAction)favoriteButtonPressed:(id)sender {

NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask ,是);
NSString * documentsDirectory = [paths objectAtIndex:0];
NSString * path = [documentsDirectory stringByAppendingPathComponent:@Object.plist];

NSMutableDictionary * mutDict = [NSMutableDictionary dictionaryWithDictionary:[detailsDataSource objectAtIndex:detailIndex]];
[mutDict setObject:@是forKey:@Done];

NSMutableArray * tmpMutArr = [NSMutableArray arrayWithArray:detailsDataSource];
[tmpMutArr replaceObjectAtIndex:detailIndex withObject:[NSDictionary dictionaryWithDictionary:mutDict]]; //在替换之前使dict不可变施放它((NSDictionary *)mutDict)也有效。

[detailsDataSource release],detailsDataSource = nil;
detailsDataSource = [[NSArray alloc] initWithArray:tmpMutArr];

[detailsDataSource writeToFile:path atomically:YES];
}






此外,而不是 @是,您可以使用 [NSNumber numberWithBool:YES]

[mutDict setObject:[NSNumber numberWithBool:YES] forKey:@Done];

通常会更好,然后使用 @是



并且:

  districtLabel.text = [[detailsDataSource objectAtIndex:detailIndex] valueForKey:@是]; 

应该是:

  districtLabel.text = [[detailsDataSource objectAtIndex:detailIndex] objectForKey:@Yes]; 






还注意到你可能内存泄漏:

  detailViewController.detailsDataSource = [[NSArray alloc] initWithArray:objectsArray]; 

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


I'm using a plist with structure like this: plist, array, dict key string key string /dict, dict key string key string /dict, /array, /plist.

What I want with the following code it to set the "Done" string's value in current dictionary to "Yes".

But now, the code replaces the whole array of dictionaries with the strings of the current dictionary (with the "Done" key valued "Yes" as it should, though.)

What would be the correct code for what I want?

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

The detailsDataSource and detailIndex are recieved from TableViewController.m segue if that matters.

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 array is used like this in detailViewController so I cannot change from array to dictionary, must make a mutable copy in that case.

解决方案

setValue:forKey is not for modifying mutable dictionaries. You should use setObject:forKey. I.e., this:

[[detailsDataSource objectAtIndex: detailIndex] setValue:@"Yes" forKey:@"Done"];

Should be:

[[detailsDataSource objectAtIndex: detailIndex] setObject:@"Yes" forKey:@"Done"];

If that doesn't fix the problem, make sure the dictionary and array are both mutable:

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


Also, instead of @"Yes", you can use [NSNumber numberWithBool:YES]:
[mutDict setObject:[NSNumber numberWithBool:YES] forKey:@"Done"];
would generally be better form then using @"Yes".

And:

districtLabel.text = [[detailsDataSource objectAtIndex: detailIndex] valueForKey:@"Yes"];

Should be:

districtLabel.text = [[detailsDataSource objectAtIndex: detailIndex] objectForKey:@"Yes"];


Also noticed that you may have a memory leak with:

detailViewController.detailsDataSource = [[NSArray alloc] initWithArray:objectsArray];

Make sure to autorelease when using property setters that retain.

这篇关于在plist字典中写入字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 17:39