I am on Xcode 8.2, OSX not iOS, Objective-C
I have an NSMutableArray and an NSIndexSet. I want to remove ALL items of the array EXCEPT those at the NSIndexSet. So basically something like
[array keepObjectsAtIndexes:indexSet]; // just to carify i know this doesnt exist
What's the best way to do this? Can i somehow 'reverse' the index set?
You can do this even without creating unnecessary arrays:
// Initial array
NSMutableArray* array = [[NSMutableArray alloc] initWithArray:@[@"one", @"two", @"three", @"four", @"five"]];
// Indices we want to keep
NSIndexSet* indexSet = [[NSIndexSet alloc] initWithIndexesInRange:NSMakeRange(1, 3)];
// Preparing reversed index set
NSMutableIndexSet *indexSetToRemove = [[NSMutableIndexSet alloc] initWithIndexesInRange:NSMakeRange(0, [array count])];
[indexSetToRemove removeIndexes:indexSet];
// Removing objects from that set
[array removeObjectsAtIndexes:indexSetToRemove];
The output will be:
2017-05-11 20:10:22.317 Untitled 15[46504:32887351] (
two,
three,
four
)
Cheers