开发者

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

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

NSIndexSet *selectedItems = [aTableView sel开发者_JS百科ectedRowIndexes];

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()


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.


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;

/**
 * Returns an array of NSNumber objects representing each index in the set.
 */
- (NSArray<NSNumber*>*) arrayOfNumbersRepresentation;

/**
 *  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];
}

/**
 * Returns an array of NSNumber objects representing each index in the set.
 */
- (NSArray<NSNumber*>*) arrayOfNumbersRepresentation {
    NSMutableArray<NSNumber*> *result = [NSMutableArray array];

    [self enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL * _Nonnull stop) {
        [result addObject:[NSNumber numberWithInteger:idx]];
    }];

    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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜