简体   繁体   English

indexsOfObjectsPassingTest:| 找到索引后,如何检查它们是否为顺序索引?

[英]indexesOfObjectsPassingTest: | once indexes have been found how to check if if they are sequential?

I am using indexesOfObjectsPassingTest successfully to get the indexes that I require. 我正在成功使用indexsOfObjectsPassingTest来获取所需的索引。

With the found indexes I would like check if any of the indexes are sequential. 使用找到的索引,我想检查是否有任何索引是连续的。 If they are I would like to get the first in that sequence and the last in the sequence. 如果是的话,我想获得该序列中的第一个和序列中的最后一个。

        NSMutableArray *domesticSetResult = [[NSMutableArray alloc]init];
        domesticSetResult = nil;

        NSMutableArray *matches = [NSMutableArray array];

        NSIndexSet *domesticIndex = [self.days indexesOfObjectsPassingTest: ^(id obj, NSUInteger idx, BOOL *stop)
                              {
                                  if ([obj isDomestic])
                                      return YES; // found a match, keep going
                                  else
                                      return NO; // keep looking
                              } ];

        [domesticIndex enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL * _Nonnull stop) {
            [matches addObject:[self.days objectAtIndex:idx]];
        }];
        NSLog(@"\n\nDomestic indexes: %@\n\n", domesticIndex);

As an example the log produces 例如,日志生成

Domestic indexes: [number of indexes: 3 (in 2 ranges), indexes: (0 3-8)] 国内索引:[索引数:3(在2个范围内),索引:(0 3-8)]

With index 0 there is only 1 in sequence so I would like it returned. 索引为0时,序列中只有1个,因此我希望它返回。 With index 3-8 I would like to return 3 and 8 对于索引3-8,我想返回3和8

I can enumerate the indexes but an not sure how to check that the indexes are in sequence and to just get the indexes that are at the beginning and end of each sequence. 我可以枚举索引,但不确定如何检查索引是否按顺序排列,以及如何获取每个序列开头和结尾处的索引。

Let NSIndexSet do the work for you. NSIndexSet为您完成工作。

For simplicity, this code constructs a second index set from the first, but it's easily adapted to do whatever you want. 为简单起见,此代码从第一个索引构建了第二个索引集,但可以轻松地对其进行修改以执行您想要的任何事情。

NSMutableIndexSet *s = [NSMutableIndexSet indexSetWithIndex:0];
[s addIndex:3];
[s addIndex:4];
[s addIndex:5];
[s addIndex:6];
[s addIndex:7];
[s addIndex:8];

NSMutableIndexSet *s2 = [[NSMutableIndexSet alloc] init];
[s enumerateRangesUsingBlock:^(NSRange range, BOOL *stop) {
  NSLog(@"%@", NSStringFromRange(range));

  [s2 addIndex:range.location];
  if (range.length > 1) {
    [s2 addIndex:range.location + range.length - 1];
  }
}];

NSLog(@"%@", s2);

The log at the end will print the 3 indexes: 0, 3, 8 最后的日志将打印3个索引:0、3、8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Swift中使用indicesOfObjectsPassingTest: - How to use indexesOfObjectsPassingTest: in Swift 设置两个变量后如何调用方法 - How to call a method once two variables have been set 一旦删除项目后如何重新填充数组 - how to repopulate an array once items have been removed swift 如何检查是否已进行自动续订订阅IOS - How to check if auto renewal subscription have been made IOS 检查是否已创建多个UIScrollViews - Check if multiple UIScrollViews have been created 从 indexOfObjectsPassingTest 排序的数组? - sorted array from indexesOfObjectsPassingTest? 如何检查并列出或打印已添加到通知中心的观察者? - How to check and list out or print the Observer which have been added into the Notification Center? 我该如何解决这个“没有为‘Apple inc.’团队找到具有 appstore 连接访问权限的帐户。” ”? - How can I fix this “No accounts with appstore connect access have been found for the team 'Apple inc.' ”? 检查对象是否已从NSCache中删除的最佳方法? - Best way to check if objects have been removed from NSCache? 使用outlet Collection检查是否已选择所有分段控件 - Using outlet Collection to check if all segmented controls have been selected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM