从 2 个字典数组中删除
我有一个从名为 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”过滤此数组,以便第二个数组 arrayHistoryDetail 具有与 arrayHistory 相同的结构,但仅包含“item 等于”1”。这些详细信息项已成功显示在 tableView 中。
然后我想从 tableView 中选择一个项目,并从 tableView 数据源 arrayHistoryDetail 中删除它(下面代码中的第 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一些建议可以让人们更容易地回答此类问题:
我的猜测是 arrayHistoryDetail 是 NSArray 的一个实例,并且您的应用在尝试向不可变实例发送可变消息时遇到了运行时错误。至少这是要检查的第一件事:确保 arrayHistoryDetail 是 NSMutableArray 的实例。
另外,我不认为有任何理由将 tempDict 设为实例变量;只需将其声明为方法内的局部变量即可。
A couple of suggestions to make it easier for people to answer this kind of question:
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 thatarrayHistoryDetail
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.有几点需要尝试:
首先,确保从 plist 文件加载的数组是可变的,即:
接下来,我不会仅将带有属性的 ivar 用于临时变量。您的代码可以看起来像这样,并避免 tempDict 的 ivar/property:
第三,确保用于生成 arrayHistoryDetail 的代码在执行过滤时不会复制 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.:
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:
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.
为什么不直接使用
?
Why not just use
??