3

I have two elements:

NSMutableArray* mruItems;
NSArray* mruSearchItems;

I have a UITableView that holds the mruSearchItems basically, and once the user swipes and deletes a specific row, I need to find ALL matches of that string inside the mruItems and remove them from there.

I haven't used NSMutableArray enough and my code gives me errors for some reason:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
    //add code here for when you hit delete
    NSInteger i;
    i=0;
    for (id element in self.mruItems) {
        if ([(NSString *)element isEqualToString:[self.mruSearchItems objectAtIndex:indexPath.row]]) {

            [self.mruItems removeObjectAtIndex:i];
        }
        else
           {
            i++;
           }
    }
    [self.searchTableView reloadData];

}    

}

Error: I see now that some of the strings are not between quotation marks (the ones in UTF8 are though)

Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSArrayM: 0x1a10e0> was mutated while being enumerated.(
    "\U05de\U05e7\U05dc\U05d3\U05ea",
    "\U05de\U05d7\U05e9\U05d1\U05d5\U05df",
    "\U05db\U05d5\U05e0\U05df",
    "\U05d1 ",
    "\U05d1 ",
    "\U05d1 ",
    "\U05d1 ",
    Jack,
    Beans,
    Cigarettes
)'
6
  • Too messy to put in a comment, editing it into the question Commented Jul 11, 2012 at 22:35
  • 2
    You are editing the collection while enumerating through it. That is your problem Commented Jul 11, 2012 at 22:38
  • I believe the quotations marks have their part in the problem? Commented Jul 11, 2012 at 22:38
  • 1
    1. I love the contents of your array 2. ASCII-compatible strings aren't presented between quotes in log output. Weird, I know. Commented Jul 11, 2012 at 22:50
  • @MorganHarris the world needs more arrays like that. Commented Jul 11, 2012 at 23:12

2 Answers 2

6

You get an exception because you are mutating a container while iterating over its elements.

removeObject: does exactly what you're looking for: removing all objects that are equal to the argument.

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle != UITableViewCellEditingStyleDelete)
        return;

    NSString *searchString = [self.mruSearchItems objectAtIndex:indexPath.row];
    [self.mruItems removeObject:searchString];
    [self.searchTableView reloadData];
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thousands of apologies to the former responder (AND first responder ;)) @Michael Dautermann. I loved Michael's solution and I learned something new from him. This answer though, is sharp and clean in my taste, Thanks!
I've removed my offending answer. I had tried to keep using the "isEqualToString:" selector as I thought "isEqual" was not functionally the same as "isEqualToString:. I was wrong about that. But I wasn't wrong about the index "i" in the original code becoming out of order in relation to the mutable array after items were removed.
4

You cannot edit the collection while enumerating through it, instead, store the indexes away, and then remove them afterwards by looping through your array of indexes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.