从 2 个字典数组中删除

发布于 2024-08-31 05:28:03 字数 1326 浏览 7 评论 0原文

我有一个从名为 arrayHistory 的 plist(如下)加载的字典数组。

<plist version="1.0">
<array>
    <dict>
        <key>item</key>
        <string>1</string>
        <key>result</key>
        <string>8.1</string>
        <key>date</key>
        <date>2009-12-15T19:36:59Z</date>
    </dict>
...

</array>
</plist>

我根据“item”过滤此数组,以便第二个数组 arrayHistoryDe​​tail 具有与 arrayHistory 相同的结构,但仅包含“item 等于”1”。这些详细信息项已成功显示在 tableView 中。

然后我想从 tableView 中选择一个项目,并从 tableView 数据源 arrayHistoryDe​​tail 中删除它(下面代码中的第 2 行) - 有效,然后我想从 tableView 本身中删除该项目(下面代码中的第 3 行) ) - 也有效。

我的问题是我还需要从原始 arrayHistory 中删除它,所以我尝试了以下操作: 创建一个临时字典作为 ivar:

NSMutableDictionary *tempDict;
@property (nonatomic, retain) NSMutableDictionary *tempDict;

然后我的想法是在第 1 行中制作一个副本,并在第 4 行中将其从原始数组中删除 。

1   tempDict = [arrayHistoryDetail objectAtIndex: indexPath.row];
2   [arrayHistoryDetail removeObjectAtIndex: indexPath.row]; 
3   [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
4   [arrayHistory removeObject:tempDict];

没有工作 有人可以指导我正确的方向吗?我认为 tempDict 是一个指针,而 removeObject 需要一个副本?我不知道。

谢谢。

I have an array of dictionaries loaded from a plist (below) called arrayHistory.

<plist version="1.0">
<array>
    <dict>
        <key>item</key>
        <string>1</string>
        <key>result</key>
        <string>8.1</string>
        <key>date</key>
        <date>2009-12-15T19:36:59Z</date>
    </dict>
...

</array>
</plist>

I filter this array based on 'item' so that a second array, arrayHistoryDetail has the same structure as arrayHistory but only contains e.g. 'item's equal to '1'. These detail items are successfully displayed in a tableView.

I then want to select an item from the tableView, and delete it from the tableView data source, arrayHistoryDetail (line 2 in the code below) - works, then I want to delete the item from the tableView itself (line 3 in the code below) - also works.

My problem is that I also need to delete it from the original arrayHistory, so I tried the following: created a temporary dictionary as an ivar:

NSMutableDictionary *tempDict;
@property (nonatomic, retain) NSMutableDictionary *tempDict;

Then my thinking was to make a copy in line 1 and remove it from the original array in line 4.

1   tempDict = [arrayHistoryDetail objectAtIndex: indexPath.row];
2   [arrayHistoryDetail removeObjectAtIndex: indexPath.row]; 
3   [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
4   [arrayHistory removeObject:tempDict];

Didn't work. Could someone please guide me in the right direction. I'm thinking that tempDict is a pointer and that removeObject needs a copy? I don't know.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

三生池水覆流年 2024-09-07 05:28:03

一些建议可以让人们更容易地回答此类问题:

  1. 不要只是说“不起作用”,而是提供一些有关问题性质的详细信息。它是否导致运行时错误?如果是这样,并且控制台上有错误消息,请尝试将其包含在您的帖子中。
  2. 在示例中包含实例变量和方法的声明。在这种情况下,了解如何声明 arrayHistoryDe​​tail 会很有帮助。

我的猜测是 arrayHistoryDe​​tail 是 NSArray 的一个实例,并且您的应用在尝试向不可变实例发送可变消息时遇到了运行时错误。至少这是要检查的第一件事:确保 arrayHistoryDe​​tail 是 NSMutableArray 的实例。

另外,我不认为有任何理由将 tempDict 设为实例变量;只需将其声明为方法内的局部变量即可。

A couple of suggestions to make it easier for people to answer this kind of question:

  1. Instead of just saying "didn't work", provide some details on the nature of the problem. Did it result in a runtime error? If so, and there was an error message on the console, try including that in your posting.
  2. Include declarations of instance variables and methods in the example. In this case, it would be helpful to know how arrayHistoryDetail was declared.

My guess is that arrayHistoryDetail is an instance of NSArray, and that your app encountered a runtime error trying to send a mutable message to an immutable instance. At least that would be the first thing to check: make sure that arrayHistoryDetail is an instance of NSMutableArray.

Also, I don't see any reason to make tempDict an instance variable; just declare it as a local variable inside your method.

猫七 2024-09-07 05:28:03

有几点需要尝试:

首先,确保从 plist 文件加载的数组是可变的,即:

NSMutableArray * arrayHistory;

接下来,我不会仅将带有属性的 ivar 用于临时变量。您的代码可以看起来像这样,并避免 tempDict 的 ivar/property:

NSDictionary * tempDict;
tempDict = [[arrayHistoryDetail objectAtIndex:indexPath.row] retain];
[arrayHistoryDetail removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath
                 withRowAnimation:UITableViewRowAnimationFade];    
[arrayHistory removeObject:tempDict];
[tempDict release];

第三,确保用于生成 arrayHistoryDe​​tail 的代码在执行过滤时不会复制 arrayHistory 中的数据。如果它确实复制了项目,那么 tempDict 将仅保存要删除的字典的副本,这意味着它将具有与 arrayHistory 中的实际字典不同的指针值。 removeObject 方法需要一个指向要删除的对象的精确指针。

如果复制确实出现问题,您可能已经稍微更改了数据结构。最好的选择是为 arrayHistory 中的每个字典分配一个唯一标识符,并使用该信息来确定要删除哪个字典。

A couple of things to try:

First, make sure that the array you load from the plist file is mutable, i.e.:

NSMutableArray * arrayHistory;

Next, I wouldn't use an ivar with a property just for a temporary variable. Your code could instead look something like this, and avoid the ivar/property for tempDict:

NSDictionary * tempDict;
tempDict = [[arrayHistoryDetail objectAtIndex:indexPath.row] retain];
[arrayHistoryDetail removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath
                 withRowAnimation:UITableViewRowAnimationFade];    
[arrayHistory removeObject:tempDict];
[tempDict release];

Third, be sure that the code you use to generate arrayHistoryDetail is not making a copy of the data in arrayHistory when it performs the filtering. If it does copy of the items, then tempDict will only hold a copy of the dictionary you want to delete, which means it will have a different pointer value than the actual dictionary in arrayHistory. The removeObject method requires an exact pointer to the object you intend to delete.

If the copying does turn out to be a problem, you may have change your data structure a little. The best option would be to use assign a unique identifier to each dictionary within arrayHistory, and use that information to determine which dictionary to delete.

遗失的美好 2024-09-07 05:28:03

为什么不直接使用

[arrayHistory removeObjectAtIndex: indexPath.row];

Why not just use

[arrayHistory removeObjectAtIndex: indexPath.row];

??

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文