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?
arrays can't contain nil.
An object representing a static ordered collection, for use instead of an Array constant in cases that require reference semantics.
The NSIndexSet class represents an immutable collection of unique unsigned integers, known as indexes because of the way they are used. This collection is referred to as an index set. Indexes must be in the range 0 .. NSNotFound - 1 . You use index sets in your code to store indexes into some other data structure.
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
.
NSIndexSet *selectedItems = [aTableView selectedRowIndexes];
NSMutableArray *selectedItemsArray=[NSMutableArray array];
[selectedItems enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
[selectedItemsArray addObject:[NSNumber numberWithInteger:idx]];
}];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With