views:

156

answers:

1

Hi guys, I'm just learning some basic programming in Objective C and Cocoa. I'm trying to get some data from NSTableView. Based on what I read in one tutorial, I wrote this:

NSArray * items = [[itemsTableView selectedRowEnumerator] allObjects];

But then I learned that selectedRowEnumerator was deprecated already in 10.3 Panther and that I should use selectedRowIndexes.

The problem is, I didn't find how to actually use the returned NSIndexSet to achieve the same result as with code written above.

So, If anyone could give me a tip, I would be very grateful. Thanks.

+1  A: 

You can loop through the indexes of an NSIndexSet like this:

- (void) goThroughIndexSet:(NSIndexSet *) anIndexSet
{
    NSUInteger idx = [anIndexSet firstIndex];

    while (idx != NSNotFound)
    {
        // do work with "idx"
        NSLog (@"The current index is %u", idx);

        // get the next index in the set
        idx = [anIndexSet indexGreaterThanIndex:idx];
    }

    // all done
}
dreamlax
There's also various 10.6-only methods involving blocks, but I haven't gone near blocks because I always need to support 10.4 as a minimum :(
dreamlax
Well, I've actually found a more elegant solution to my problem, but thanks for your time :)As for the blocks: Don't they work on 10.5 Leopard too? 'Cause I feel I've read somewhere that they were back-ported.
Jakub Lédl
Jakub Lédl: You should also look at the documentation for the NSIndexSet class. Every Cocoa and Cocoa Touch class has a page documenting its public interface. http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSIndexSet_Class/
Peter Hosey