我创建了一个NSIndexSet
NSIndexSet* groups = [self.specifiers indexesOfObjectsPassingTest:^(PSSpecifier*specifier, NSUInteger idx, BOOL *stop) {
return [(NSString*)[specifier.properties objectForKey:@"cell"] isEqualToString:@"KBGroupCell"]; }];
可能是这样的:
(0 3 5 8-9 12 14 17-19)
和给定的最大值为25。
我希望过滤并创建范围,以便实现如下所示的输出:
0-2
3-4
5-7
8-11
12-13
14-16
17-25
所使用的数组由用户填充,而不是由我自己填充。KBGroupCells用于填充表中的头和页脚,因此不应该连续识别。当我稍后填充表时,我需要记住忽略的数字(9、18和19)。
通过使用rangeAtIndex
,我可以识别集合中包含多个值的范围,因此可能用于查找要忽略的数字。
我怎样才能做到这一点?
发布于 2018-11-13 23:38:34
我猜你有NSIndexSet:
let original = IndexSet(arrayLiteral: 0, 3, 5).union(IndexSet(integersIn: 8...9)).union( IndexSet.init(arrayLiteral: 12,14)).union(IndexSet(integersIn: 17...19)).union(IndexSet.init(arrayLiteral: 26,27))
let nsIndex = original as! NSIndexSet
在将其转换为IndexSet之后,您可以轻松地获得两个数组。一个是NSRange,另一个是closedRange
let index = nsIndex as! IndexSet
let rangeView = index.rangeView
print ( Array(rangeView.enumerated().map{
NSRange.init(location:($0.element.first!) , length: ((rangeView[$0.offset + 1].first! - ($0.element.first!))))
}.dropLast()))
print ( Array(rangeView.enumerated().map{
($0.element.first!)...(rangeView[$0.offset + 1].first!) - 1
}.dropLast()))
如果是客观的-C。就像这样:
NSMutableIndexSet * nsIndex = [[NSMutableIndexSet alloc] init];
[nsIndex addIndex: 0];
[nsIndex addIndex: 3];
[nsIndex addIndex: 5];
[nsIndex addIndexesInRange:NSMakeRange(8, 2)];
[nsIndex addIndex: 12];
[nsIndex addIndex: 14];
[nsIndex addIndexesInRange:NSMakeRange(17, 3)];
[nsIndex addIndex: 26];
__block int count = 0;
__block NSMutableArray * ranges = [NSMutableArray array];
[nsIndex enumerateRangesUsingBlock:^(NSRange range, BOOL * _Nonnull stop) {
count ++;
[ranges addObject:[NSValue valueWithRange: range]];
}];
NSMutableArray * result = [NSMutableArray array];
for (NSUInteger location = 0 ; location < count - 1 ; location++) {
NSUInteger loc = ((NSValue *) ranges[location]).rangeValue.location;
[result addObject:[NSValue valueWithRange: (NSMakeRange( loc, ((NSValue *) ranges[location + 1]).rangeValue.location - loc))]];
}
NSLog(result.description);
或者用一轮:
__block NSUInteger temp = nsIndex.firstIndex;
__block NSMutableArray * result = [NSMutableArray array];
[nsIndex enumerateRangesWithOptions:NSEnumerationReverse usingBlock:^(NSRange range, BOOL * _Nonnull stop) {
[result insertObject: [NSValue valueWithRange: NSMakeRange(range.location, temp - range.location)] atIndex:0];
temp = range.location ;
}];
result = [result subarrayWithRange:NSMakeRange(0, result.count - 1)];
NSLog(result.description);
发布于 2018-11-13 22:12:07
这是一个可能的算法,编码和一些错误检查是留作练习!
cursor <- theSet first contained index
result <- empty array
while cursor <= maxIndex
start <- cursor
while cursor <= maxIndex and theSet contains cursor do increment cursor
while cursor <= maxIndex and theSet does not contain cursor do increment cursor
newRange <- start to cursor-1
add newRange to result
end
return result
该算法仅使用两种NSIndexSet
方法--一种是查找集合中的第一个索引,另一种是测试索引是否在集合中。有一些“更高层次”的方法可以加快算法的速度,但是在0-25的范围内通过索引就足够了。
HTH
https://stackoverflow.com/questions/53289230
复制相似问题