如何从ios中的plist文件中删除特定数据

时间:2014-05-19 11:30:33

标签: ios

我无法从plist文件中删除特定数据。我在stackoverflow中搜索了这么多答案,但我无法解决我的问题。

这是我的代码:

ViewDidLoad我从plist文件中检索数据。

- (void)viewDidLoad
{
   [super viewDidLoad];

   NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
   NSString *documentsPath = [paths objectAtIndex:0];
   NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"manuallyData.plist"];

   if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath])
   {
       plistPath = [[NSBundle mainBundle] pathForResource:@"manuallyData" ofType:@"plist"];
   } 

   NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:plistPath];

   self.nameArr = [dict objectForKey:@"Name"]; // Here i got all my name
   self.countryArr = [dict objectForKey:@"Country"]; // here i got all my country
}  

我删除的代码:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0];
    NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"manuallyData.plist"];
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithContentsOfFile:(NSString *)plistPath];

    NSString *key = [self.nameArr objectAtIndex:indexPath.row];
    [dictionary removeObjectForKey:[NSString stringWithFormat:@"%@", key]];


    [dictionary writeToFile:plistPath atomically:YES];
    self.nameArr = [dictionary allKeys];

    NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
    [tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
 }

当我构建此应用程序时,我收到此错误:

 Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0.  The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (4), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

请有人建议我,我该怎么做?

感谢高级。 :)

2 个答案:

答案 0 :(得分:0)

我猜你的根本原因在于这两行。不是很确定

self.nameArr = [dict objectForKey:@"Name"]; // Here i got all my name  

self.nameArr = [dictionary allKeys]; //I can see your dictionary contains only two keys, Name and Country 

我会稍微说服一下。首先,您使用“名称”键获取您的姓名。稍后您将获得与self.nameArr = [dictionary allKeys];相同的名称数组。你有所不同?在第二种情况下,您的数组包含键[Name, Country]。我刚刚解释了导致错误的代码。

然后您编写的删除代码也不正确。您已经像最初一样获得了名称和国家/地区数组。然后从那里删除,写回来。每次读写文件都不好。它会影响应用程序的性能。

答案 1 :(得分:0)

与Joe Blow一样,我认为问题在于您如何管理表格视图。

尝试将[tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic]替换为[tableView reloadData],看看会发生什么。

文件未更新的原因可能是因为在写入文件之前抛出了异常。

现在解决您的问题,检查numberOfRowsInSection:的实施情况。这可能就是错误所在。

另外,检查[dictionary writeToFile:plistPath atomically:YES]的返回。你不应该假设文件是​​写的。

if ([dictionary writeToFile:plistPath atomically:YES]) {
    self.nameArr = [dictionary allKeys];
    NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
    [tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
} else {
    NSLog(@"Failed saving %@", plistPath);
}