単体テストでいくつかの境界条件を調べていたところ、テストが失敗し続けました。インデックスが 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 シミュレーターでこれを試したところ、同じ結果が得られました (32 ビットまたは 64 ビットに応じて NSNotFound の実際の値を除く)。
これは、ドキュメントを間違って読んでいたのではないかと思いました。私は何か間違ったことをしていますか、それともこれは Apple のバグですか?