Friday, June 21, 2013

NSMutableIndexSet addIndexesInRange example in Objective C (iOS).


NSMutableIndexSet addIndexesInRange

Adds the indexes in an index range to the receiver.

- (void)addIndexesInRange:(NSRange)indexRange

Parameters
indexRange
Index range to add. Must include only indexes representable as unsigned integers.

Discussion of [NSMutableIndexSet addIndexesInRange]
This method raises an NSRangeException when indexRange would add an index that exceeds the maximum allowed value for unsigned integers.

NSMutableIndexSet addIndexesInRange example.
//Not tested
//Assume that selectedIndexes is a NSMutableIndexSet
if ([selectedIndexes containsIndex:indexPath.row]) {
    if (self.options.count) {//This could probably be optimised depend on your goal
        [selectedIndexes addIndexesInRange:NSMakeRange(0,self.options.count-1)];
    } else {
        [selectedIndexes removeAllIndexes];
    }

Example of [NSMutableIndexSet addIndexesInRange].
@autoreleasepool {
      
        // insert code here...
        NSLog(@"Hello, World!");
        NSRange indexRange = NSMakeRange(0, 6);
        NSMutableIndexSet *newIndexSet = [[NSMutableIndexSet alloc] initWithIndexesInRange:indexRange];
        for (int i=0; i<40; i++)
        {
            NSUInteger x = [newIndexSet oneTimeRandomIndex];
            if (x == NSNotFound)
            {
                                       // NSLog(@"newIndexSet needs to be refilled");
                [newIndexSet addIndexesInRange:indexRange];
                x = [newIndexSet oneTimeRandomIndex];
              
            }
            switch (x) {
                case 0:
                    NSLog(@"Cat");
                    break;
                case 1:
                    NSLog(@"Dog");
                    break;
                case 2:
                    NSLog(@"Fish");
                    break;
                case 3:
                    NSLog(@"Horse");
                    break;
                case 4:
                    NSLog(@"Bird");
                    break;
                case 5:
                    NSLog(@"Wombat");
                    break;  
                default:
                    break;
            }
          
        }

         NSLog(@"See Ya Later, World!");
    }
    return 0;

End of NSMutableIndexSet addIndexesInRange example article.