views:

120

answers:

3

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.

A: 

Why not just use

[arrayHistory removeObjectAtIndex: indexPath.row];

??

Pyro2927
because indexPath.row will have the index position of the object in arrayHistoryDetail which is much smaller than arrayHistory and therefore the object that I want to delete is in a different position so I'd end up deleting some random object from arrayHistory
Matt Winters
Sorry, Pyro2927. The -1 is from me. As pointed out by the OP, this will not fail to delete the appropriate item.
e.James
+1  A: 

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.

jlehr
Thank you for your suggestions. I agree with everything you said. I considered putting more detail but thought it was too big already, but you're right I should have answered what you asked, without you having to ask. arrayHistoryDetail was a NSMutableArray; build was fine, runtime error with no error messages, none; debugger pointed to random line - memory issue?; was trying something else as a fix in different method and made tempDict an ivar - should have changed it back. When I did change it to look like e.James suggested, it worked. The only thing I didn't have before was the retain.
Matt Winters
A: 

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.

e.James
Not sure why this worked but Thank you. I thought I was here before, the only difference being the retain. For clarification, I changed tempDict back to a temporary variable first, then it worked but not for more than a few deleted cells, then the debugger started to point to random lines, added the retain and it become much more stable. I'm going to check around for any other memory issues that could have contributed 'cause I really don't understand how this retain could have fixed it - but it did so maybe I should just let it go. Thanks again.
Matt Winters
In your original code, when you set `tempDict = ...`, the value is not actually being retained. You set the property type of tempDict to retain, but you bypassed the property accessor, so it had no effect. To use your original code as-is, you would have to use either `self.tempDict = ...` or `[self setTempDict:...]`.
e.James