How to get indexes from NSIndexset into an NSArray in cocoa?

Mr_Nizzle picture Mr_Nizzle · Sep 22, 2010 · Viewed 20.3k times · Source

I'm getting the select items from a table view with:

NSIndexSet *selectedItems = [aTableView selectedRowIndexes];

what's the best way to get the indexes in a NSArray object?

Answer

Chuck picture Chuck · Sep 22, 2010

Enumerate the set, make NSNumbers out of the indexes, add the NSNumbers to an array.

That's how you'd do it. I'm not sure I see the point in transforming a set of indexes into a less efficient representation, though.

To enumerate a set, you have two options. If you're targeting OS X 10.6 or iOS 4, you can use enumerateIndexesUsingBlock:. If you're targeting earlier versions, you'll have to get the firstIndex and then keep asking for indexGreaterThanIndex: on the previous result until you get NSNotFound.