【问题标题】:Write filtered NSArray back to plist将过滤后的 NSArray 写回 plist
【发布时间】:2012-06-14 20:38:50
【问题描述】:

我将 plist 加载到 NSArry,过滤该数组以捕获我需要的数据,然后将数据呈现给 UITableView。这很好用。

编辑:

我正在处理字典数组。 Plist 充满了字典数组。

这就是我的工作:

NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"/data.plist"];
//array is filled with dictionaries
array = [NSMutableArray arrayWithContentsOfFile:filePath];

//filter the array to catch appropriate data (there are maybe 10 objects):
self.arrayFiltered = [NSMutableArray arrayWithCapacity:[array count]];
NSDictionary *dict;
for (dict in array)
    if ([[dict valueForKey:@"globalKey"] isEqualToNumber:[NSNumber numberWithInt:year]])
        [arrayFiltrirani addObject:dict];

现在我用 arrayFiltered 中的数据填充 tableView。现在我想从 tableView 和 arrayFiltered 中删除一行。

[self.arrayFiltrirani removeObjectAtIndex:indexPath.row];

但是!!!如何更新原始数组并将其保存到 plist?

【问题讨论】:

  • 你的实际意图是什么?您提供的代码与您所说的完全一样。它需要一个数组并将其转换为 .plist。您要保留现有文件内容以及新文件内容吗?那是 NSDictionary 更适合的东西。
  • 当我将过滤后的数组保存到 plist 时,之前的所有数据都会被覆盖,现在 plist 中只有来自 arrayFiltered 的数据。

标签: iphone uitableview nsarray plist


【解决方案1】:

由于您使用的是字典,因此您需要将实际的字典容器对象写回到 data.plist 文件中,所以

NSDictionary *myDictionary = [masterArray objectAtIndex:indexOfDictionary];
NSMutableArray *myDataArray = [myDictionary objectForKey:@"dataArray"];
[myDataArray removeObject:objRemovedFromTable]; //or removeObjectAtIndex:indexPath.row
NSString *filePath = [docsDirectory stringByAppendingPathComponent:@"data.plist"];
[masterArray writeToFile:filePath atomically:YES];

【讨论】:

  • 对不起,我不明白我要做什么?在我的数组中,我有带有字典的孔 .plist。在我的 arrayFiltered 中,我有 tableView 中显示的字典(只有少数,而不是洞 .plist !!)。那么我应该将原始数组还是 arrayFiltered 保存到 .plist (writeToFile:filePath) 中?如果我从 arrayFiltered 中删除对象并保存它,它将替换其他所有内容。如何在原始数组中找到我要删除的字典?
  • 我没有看到你编辑了你的答案。如何获取 indexOfDictionary?
  • 只有你能回答这个问题。您如何访问填充 tableview 的数据?关键是,您必须将容器文件写回磁盘,无论对象存储您正在使用的所有字典。
  • 你应该做的是为主数组实现一个属性,比如@property (nonatomic, strong) NSMutableArray *mainArray;像这样加载它: self.mainArray = [NSMutableArray arrayWithContentsOfFile:filePath];然后只需调用 [mainArray writeToFile:filePath atomically:YES];
【解决方案2】:

我已经设法解决了我的问题。基本上我从主数组中制作了 2 个数组,“过滤”数组和“其他”数组。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"/data.plist"];

array = [NSMutableArray arrayWithContentsOfFile:filePath];

self.arrayFiltered = [NSMutableArray arrayWithCapacity:[array count]];
self.arrayOther = [NSMutableArray arrayWithCapacity:[array count]];

NSDictionary *dict;
for (dict in array)
    if ([[dict valueForKey:@"globalKey"] isEqualToNumber:[NSNumber numberWithInt:godinaMjesec]])
        [arrayFiltered addObject:dict];
    else {
        [arrayOther addObject:dict];
    }

现在,当我想从 arrayFiltered(tableView 从中获取数据)中删除数据时:

[self.arrayFiltered removeObjectAtIndex:indexPath.row];

重写原始数组:

[self.arrayOther addObjectsFromArray:self.arrayFiltered];
[self.arrayOther writeToFile:filePath atomically:YES];

这可能不是正确的方法,但它对我有用。

【讨论】:

    【解决方案3】:

    如果它是一个需要一段时间才能写入的大型 plist 文件,那么您可能使用了错误的工具。我会使用 Core Data 来删除单独的数据行。

    【讨论】:

      猜你喜欢
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-11
      相关资源
      最近更新 更多