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

后端 未结 5 1624
Happy的楠姐
Happy的楠姐 2020-12-11 01:20

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

NSIndexSet *selectedItems = [aTableView selectedRowIndexes];

what\'s the best way to

相关标签:
5条回答
  • 2020-12-11 01:32

    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.

    0 讨论(0)
  • 2020-12-11 01:34

    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()
    
    0 讨论(0)
  • 2020-12-11 01:36

    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
    
    0 讨论(0)
  • 2020-12-11 01:41
    NSIndexSet *selectedItems = [aTableView selectedRowIndexes];
    
    NSMutableArray *selectedItemsArray=[NSMutableArray array];
        [selectedItems enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
            [selectedItemsArray addObject:[NSNumber numberWithInteger:idx]];
        }];
    
    0 讨论(0)
  • 2020-12-11 01:50

    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.

    0 讨论(0)
提交回复
热议问题