【问题标题】:How to delete a specific data from plist file in ios如何从ios中的plist文件中删除特定数据
【发布时间】:2014-07-07 08:19:04
【问题描述】:

我无法从我的 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).'

请任何人建议我,我必须做什么?

感谢高级版。 :)

【问题讨论】:

  • 您使用什么数据作为数据源? self.nameArr?你能显示numberOfRowsInSection代码吗
  • 你好,anil,我只使用你的代码,它显示错误。好的,我会分享,请帮助我
  • -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.nameArr count]; }
  • self.nameArr 是一个 NSArray。我是属性合成这个
  • 实际上您要删除什么?名字还是国家?

标签: ios


【解决方案1】:

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

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]。我只是解释了导致错误的代码。

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

【讨论】:

  • 雅阿尼尔,我遵循你的代码。你写 viewerKeys = [dictionary allKeys];但我无法理解“viewerKeys”。所以我取 self.nameArr = [dictionary allKeys];。你能解释一下这个 viewerKeys 是什么,否则你是对的。你明白我的意思
  • 我不知道你引用的是我的哪个代码,它可能是为其他问题编写的,它可能只在这种情况下有效
【解决方案2】:

和 Joe Blow 一样,我认为问题在于您管理表格视图的方式。

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

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

现在解决您的问题,检查您对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);
}

【讨论】:

    猜你喜欢
    • 2019-06-02
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-21
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多