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

后端 未结 5 1635
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: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
    

提交回复
热议问题