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?
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]];
}];
With swift you can do the following
extension NSIndexSet {
func toArray() -> [Int] {
var indexes:[Int] = [];
self.enumerateIndexesUsingBlock { (index:Int, _) in
indexes.append(index);
}
return indexes;
}
}
then you can do
selectedItems.toArray()
I did it by creating a category on NSIndexSet. This kept it small and efficient, requiring very little code on my part.
My interface (NSIndexSet_Arrays.h):
/**
* Provides a category of NSIndexSet that allows the conversion to and from an NSDictionary
* object.
*/
@interface NSIndexSet (Arrays)
/**
* Returns an NSArray containing the contents of the NSIndexSet in a format that can be persisted.
*/
- (NSArray*) arrayRepresentation;
/**
* Initialises self with the indexes found wtihin the specified array that has previously been
* created by the method @see arrayRepresentation.
*/
+ (NSIndexSet*) indexSetWithArrayRepresentation:(NSArray*)array;
@end
and the implementation (NSIndexSet_Arrays.m):
#import "NSIndexSet_Arrays.h"
@implementation NSIndexSet (Arrays)
/**
* Returns an NSArray containing the contents of the NSIndexSet in a format that can be persisted.
*/
- (NSArray*) arrayRepresentation {
NSMutableArray *result = [NSMutableArray array];
[self enumerateRangesUsingBlock:^(NSRange range, BOOL *stop) {
[result addObject:NSStringFromRange(range)];
}];
return [NSArray arrayWithArray:result];
}
/**
* Initialises self with the indexes found wtihin the specified array that has previously been
* created by the method @see arrayRepresentation.
*/
+ (NSIndexSet*) indexSetWithArrayRepresentation:(NSArray*)array {
NSMutableIndexSet *result = [NSMutableIndexSet indexSet];
for (NSString *range in array) {
[result addIndexesInRange:NSRangeFromString(range)];
}
return result;
}
@end
Here is the sample code:
NSIndexSet *filteredObjects = [items indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {do testing here}];
NSArray *theObjects = [theItems objectsAtIndexes:filteredObjects]
Availability Available in iOS 2.0 and later.
来源:https://stackoverflow.com/questions/3773180/how-to-get-indexes-from-nsindexset-into-an-nsarray-in-cocoa