NSIndexSet indexPassingTest example

83 Views Asked by At

I can't seem to find a suitable example of how to use "indexPassingTest" with an NSIndexSet. I am trying to compare to NSIndexSets, in order to find the difference. I was hoping to use the following code:

NSIndexSet *selectionIndexesNew = [change objectForKey:@"new"];
NSIndexSet *selectionIndexesOld = [change objectForKey:@"old"];
NSUInteger newSelection = 0;
newSelection = [selectionIndexesNew indexPassingTest:^BOOL(NSUInteger idx, BOOL *stop) {
    *stop = NO;
    if ([selectionIndexesOld containsIndex:idx] == NO) {
        *stop = YES;
    }
    return idx;
}];

This doesn't seem to iterate through my NSIndexSet at all. The first value from selectionIndexesNew is used, but subsequent values are ignored. My understanding of this method is that all Index values are iterated. Or am I wrong on this?

1

There are 1 best solutions below

3
Bob-GlueTools On

Ok, figured it out, right after posting this.. You have to return NO if your idx doesn't match. Passing YES if the index matches.

Here is code that seems to work. (This has not been extensively tested..)

NSIndexSet *selectionIndexesNew = [change objectForKey:@"new"];
NSIndexSet *selectionIndexesOld = [change objectForKey:@"old"];
NSUInteger newSelection = 0;
newSelection = [selectionIndexesNew indexPassingTest:^BOOL(NSUInteger idx, BOOL *stop) {
    if ([selectionIndexesOld containsIndex:idx] == NO) {
        *stop = YES;
        return YES;
    }
    return NO;
}];