在单元测试中,我正在查看一些边界条件,但测试一直失败。当索引的索引一直扩展到NSNotFound-1(每个文档的最大合法值)时,我将其追溯到枚举整个索引集。

使用此测试代码:

// Create an index set with NSNotFound-5, NSNotFound-4, NSNotFound-3,
// NSNotFound-2 and NSNotFound-1
NSIndexSet *testSet = [NSIndexSet indexSetWithIndexesInRange:
    NSMakeRange( NSNotFound - 5, 5 )];
NSLog( @"Index set with %lu entries: %@", (unsigned long) testSet.count, testSet );
NSLog( @"NSNotFound is %lu and NSNotFound-1 is %lu", NSNotFound, NSNotFound - 1 );
__block int count = 0;
[testSet enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
    NSLog( @"- %lu", (unsigned long) idx );
    ++count;
}];
NSLog( @"The count is %d", count );

在日志输出中,我得到:
Index set with 5 entries: <NSIndexSet: 0x10057f890>[number of indexes: 5 (in 1 ranges), indexes: (9223372036854775802-9223372036854775806)]
NSNotFound is 9223372036854775807 and NSNotFound-1 is 9223372036854775806
- 9223372036854775802
- 9223372036854775803
- 9223372036854775804
- 9223372036854775805
The count is 4

我在Mac和iOS模拟器上尝试了此操作,并得到了相同的结果(除了NSNotFound的实际值,取决于32位还是64位)。

这让我觉得也许我在阅读文档时出错了。我是在做错什么,还是这是Apple的错误?

最佳答案

这不是错误。您设置的值超出了验证值。 “NSIndexSet类表示唯一的无符号整数的不可变集合,由于使用它们的方式而被称为索引。该集合称为索引集。索引的范围必须为0 .. NSNotFound-1。”这是link!

10-04 10:05