Monday, June 17, 2013

NSIndexSet indexGreaterThanOrEqualToIndex example in Objective C (iOS).


NSIndexSet indexGreaterThanOrEqualToIndex

Returns either the closest index in the index set that is greater than or equal to a specific index or the not-found indicator.

- (NSUInteger)indexGreaterThanOrEqualToIndex:(NSUInteger)index

Parameters
index
Index being inquired about.

Return Value of [NSIndexSet indexGreaterThanOrEqualToIndex]
Closest index in the index set greater than or equal to index; NSNotFound when the index set contains no qualifying index.

NSIndexSet indexGreaterThanOrEqualToIndex example.
Usually, you enumerate through the indexes in a set like so:

NSUInteger idx = [theSet indexGreaterThanOrEqualToIndex: 0];
while (idx != NSNotFound) {
    // idx equals the next index in the set.
    idx = [theSet indexGreaterThanIndex: idx];
}

Example of [NSIndexSet indexGreaterThanOrEqualToIndex].
@interface IKImageBrowserView (event)

- (void)mouseDown:(NSEvent *)theEvent ;

@end

@implementation IKImageBrowserView (event)

- (void)mouseDown:(NSEvent *)theEvent
{
    NSPoint pt = [self convertPoint: theEvent.locationInWindow fromView: nil];

    NSInteger index = [self indexOfItemAtPoint:pt] ;

    if ( index != NSNotFound )
    {
    NSUInteger ge ;
    NSUInteger le ;
    NSIndexSet* set = [self selectionIndexes] ;
    NSMutableIndexSet* mutableSet = [[NSMutableIndexSet alloc] init] ;

    [mutableSet addIndexes:set] ;

    ge = [mutableSet indexGreaterThanOrEqualToIndex:index] ;
    le = [mutableSet indexLessThanOrEqualToIndex:index] ;

    if ( (ge == le) && (ge != NSNotFound) )
    {
        [mutableSet removeIndex:index] ;
    }
    else
    {
        [mutableSet addIndex:index] ;
    }

    [self setSelectionIndexes:mutableSet byExtendingSelection:NO] ;
//      [ mutableSet release ];
    }
}

@end

End of NSIndexSet indexGreaterThanOrEqualToIndex example article.