Deleting from 2 arrays of dictionaries
I have an array of dictionaries loaded from a plist (below) called array开发者_C百科History.
<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.
A couple of suggestions to make it easier for people to answer this kind of question:
- 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.
- 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.
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.
Why not just use
[arrayHistory removeObjectAtIndex: indexPath.row];
??
精彩评论