ios - 这是 NSIndexSet enumerateIndexesUsingBlock 中的错误吗?

标签 ios macos cocoa nsindexset

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

关于ios - 这是 NSIndexSet enumerateIndexesUsingBlock 中的错误吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23712904/

相关文章:

ios - Swift 2 SkScene 更改

ios - 导航标题背景图片不起作用

python - 尝试在 OS X (10.11.3) 上安装 PyAudio

cocoa - 最简单、简约、opengl 3.2 cocoa项目

ios - NSScrollView 中的 NSPopoverTouchBarItems (NSTouchBar)

ios - SWRevealViewController panGesture 问题 - IOS/Swift

iphone - textFieldDidEndEditing 替代方案

macos - 如何实现带有下拉菜单的工具栏按钮?

android - Cocos2D-X for android, Symbol 'cocos2d' 无法解析

java - 阻止用户离开 OS X 上的处理应用程序?