【问题标题】:Is this a bug in NSIndexSet enumerateIndexesUsingBlock?这是 NSIndexSet enumerateIndexesUsingBlock 中的错误吗?
【发布时间】:2014-05-17 15:13:17
【问题描述】:

在我的单元测试中,我查看了一些边界条件,但我的测试一直失败。当它的索引一直延伸到 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 的错误?

【问题讨论】:

  • 这是一个有趣的问题。 +1
  • 这看起来真的像 enumerateIndexesWithOptions 中的一个错误。
  • 如果范围从NSNotFound - 6 开始(并保持长度为5),是否有效?
  • 是的,按预期结果是 5。
  • 我认为这实际上是 NSIndexSet 中一个更广泛的错误;它的所有枚举方法都有相同的问题。当然值得提交错误报告。也就是说,我很好奇这是怎么出现的,以及它是否可能影响实际代码——我敢肯定,分配 9223372036854775807 个对象的数组不会有太多运气。 :-)

标签: ios macos cocoa nsindexset


【解决方案1】:

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

【讨论】:

  • 我已阅读文档,我的索引是:NSNotFound-5、NSNotFound-4、NSNotFound-3、NSNotFound-2 和 NSNotFound-1,都是 > 0 和 [NSIndexSet indexSetWithIndexesInRange: NSMakeRange( NSNotFound - 5, 6)],它会生成错误*** -[NSIndexSet initWithIndexesInRange:]: Range {9223372036854775802, 6} exceeds maximum index value of NSNotFound - 1。我没有超过最大值,我很确定枚举方法被破坏了。
  • 对不起,一开始我误解了你的意思。我已经测试了那个代码,我认为这是关于边界值,这是一个错误。
【解决方案2】:

看来我已经确认它是/曾经是一个错误。我提交了一份错误报告,并从 Apple 那里听说它已在最新的 OS X Yosemite 开发者预览版和 Xcode 预览版中得到修复。我下载了它,它似乎按文档中的预期工作。

我怀疑他们必须更改一行代码,所以问他们是否会修补当前或以前版本的 Xcode,但我没有听说过,也没有屏住呼吸。

【讨论】:

    猜你喜欢
    • 2013-05-04
    • 2011-09-28
    • 2017-07-23
    • 2012-09-15
    • 2017-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多