The collection was mutated during the listing, UITableView

I have a filter button that represents a UITableView in a popover. I have my categories and the "All" button to indicate that the filter is not present, as in iTunes.

I have an NSMutableDisctionary in my applicationDelegate class that I use to set checkboxes. When the application starts, only All is selected, everything else is canceled. What I want is when a row that is not β€œAll” is selected, that row is selected and everything becomes inaccessible. Similarly, when All is selected, all lines with checkmarks no longer have checkmarks, and only All is selected using a checkmark (for example, when the application starts). In my UITableView didSelectRowForIndexPath :, I did this:

MyAppAppDelegate *dmgr = (MyAppAppDelegate *)[UIApplication sharedApplication].delegate; NSUInteger row = [indexPath row]; UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; // All selected if (row == 0) { for (NSString *key in dmgr.CategoryDictionary) { [dmgr.CategoryDictionary setObject:[NSNumber numberWithBool:NO] forKey:key]; } [dmgr.CategoryDictionary setObject:[NSNumber numberWithBool:YES] forKey:@"All"]; } else { cell.accessoryType = UITableViewCellAccessoryCheckmark; NSString *key = [_categoriesArray objectAtIndex:row]; BOOL valueAtKey = [[dmgr.CategoryDictionary valueForKey:key] boolValue]; valueAtKey = !valueAtKey; [dmgr.CategoryDictionary setObject:[NSNumber numberWithBool:valueAtKey] forKey:key]; } 

Two questions. Firstly, I get this error when I select the first row (all):

 Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSCFDictionary: 0x597b3d0> was mutated while being enumerated. 

Where does the transfer take place? I thought, since I only select line 0, I could change other lines, not just line 0. I am not sure what to do with this here.

The second question is how do you want to update your model class? I was not sure if this was considered a good MVC. Thanks.

+7
source share
1 answer

An enumeration is a for loop. You can iterate over a copy of the keys instead to avoid changing the dictionary when listing it:

 for (NSString *key in [dmgr.CategoryDictionary allKeys]) { //... } 
+19
source

All Articles