Edit AllPages

When removing a set of selected objects from a mutable array, check for object identity, not simply equality, w.r.t. the base array

Here’s a familiar class for implementing a simple table view, given in its entirety; I made a slight modification to it to use an NSIndexSet when deleting multiple records from the table. If all the records are unique, things work fine, but if two records contain dictionaries with identical content, all records with that content will be deleted, ignoring whether they were selected before deletion.

I expected that the enumerator for adding the objects to a temp array before deleting will only add the objects that I selected.

#import <Cocoa/Cocoa.h>

@interface BTVController: NSObject { IBOutlet NSTextField *addressField; IBOutlet NSTextField *emailField; IBOutlet NSTextField *firstNameField; IBOutlet NSTextField *lastNameField; IBOutlet NSTableView *tableView;

NSMutableArray *records; }


#import “BTVController.h”

@implementation BTVController

// NSTableDataSource methods

// NSTableDataSource method that we implement to edit values directly in the table…


Why are you using an object enumerator when you are stepping the variable “i” to do the same thing?

int count = [records count];
while (count--) {
    if ([set containsIndex:count]) [tempArray addObject:[records objectAtIndex:count]];

Actually the better thing to do would be this;

if ([set count]) {
    int i = [set firstIndex];
          [tempArray addObject: [records objectAtIndex:i]];
    while        ((i = [set indexGreaterThanIndex:i]) != NSNotFound);

I expect it’s because you’re removing based on equality, not identity. Try going through the array of objects to remove and use removeObjectIdenticalTo: