【问题标题】:How to get indexes from NSIndexset into an NSArray in cocoa?如何从 NSIndexset 获取索引到可可中的 NSArray?
【发布时间】:2010-09-22 20:07:02
【问题描述】:

我从表格视图中获取选择项:

NSIndexSet *selectedItems = [aTableView selectedRowIndexes];

在 NSArray 对象中获取索引的最佳方法是什么?

【问题讨论】:

  • 我有一个方法获取一个 NSArray 作为参数来删除从 tableview 中选择的多个记录
  • 这似乎有点循环。如果数组的目的是包含一组索引,为什么不让该方法取一个 NSIndexSet 呢?
  • 在 NSArray 上没有一个方法可以返回提供的 NSIndexSex 中的元素吗?

标签: objective-c cocoa nsindexset


【解决方案1】:

枚举集合,从索引中生成 NSNumbers,将 NSNumbers 添加到数组中。

你就是这样做的。不过,我不确定我是否明白将一组索引转换为效率较低的表示的意义。

要枚举一个集合,您有两个选择。如果您的目标是 OS X 10.6 或 iOS 4,您可以使用enumerateIndexesUsingBlock:。如果您针对的是早期版本,则必须获取 firstIndex,然后在之前的结果中继续询问 indexGreaterThanIndex:,直到您得到 NSNotFound

【讨论】:

  • 你说得对,只是我不知道如何使用 while 循环或 foreach 循环从 NSIndexSet 获取索引......
  • @Chuck 这样做的一个原因是允许 NSIndexSet 在 NSDictionary 中持久存在。是的,您也可以通过将其归档到 NSData 中来实现这一点,但这也不是很好。
  • 另一个原因是对索引进行洗牌。
  • 还有一个按索引访问索引的方法。
【解决方案2】:
NSIndexSet *selectedItems = [aTableView selectedRowIndexes];

NSMutableArray *selectedItemsArray=[NSMutableArray array];
    [selectedItems enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
        [selectedItemsArray addObject:[NSNumber numberWithInteger:idx]];
    }];

【讨论】:

    【解决方案3】:

    使用 swift 您可以执行以下操作

    extension NSIndexSet {
        func toArray() -> [Int] {
            var indexes:[Int] = [];
            self.enumerateIndexesUsingBlock { (index:Int, _) in
                indexes.append(index);
            }
            return indexes;
        }
    }
    

    那你就可以了

    selectedItems.toArray()
    

    【讨论】:

      【解决方案4】:

      我通过在 NSIndexSet 上创建一个类别来做到这一点。这使它保持小而高效,我只需要很少的代码。

      我的界面(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
      

      和实现(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
      

      【讨论】:

        【解决方案5】:

        这里是示例代码:

        NSIndexSet *filteredObjects = [items indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {do testing here}];
        
        NSArray *theObjects = [theItems objectsAtIndexes:filteredObjects]
        

        可用性 适用于 iOS 2.0 及更高版本。

        【讨论】:

          猜你喜欢
          • 2010-10-28
          • 2011-11-15
          • 1970-01-01
          • 1970-01-01
          • 2013-12-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多