indexSetWithIndexesInRange没有做到预期的事情

tes*_*ing 5 iphone cocoa-touch objective-c nsindexset nsrange

我想从数组中选择一些对象.因此我正在使用我选择的开始和结束索引.

NSLog(@"start:%d\nend:%d", startIndex, endIndex);
NSIndexSet *myIndexes = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(startIndex, endIndex)];
NSLog(@"%d", [myIndexes lastIndex]);
Run Code Online (Sandbox Code Playgroud)

第一个NSLog给了我

startIndex:49
endIndex:67

第二个NSLog给了我

115

为什么我的115号码数量最多?它应该是67.当然应用程序崩溃:

由于未捕获的异常'NSRangeException'而终止应用程序,原因:' * - [NSArray objectsAtIndexes:]:索引115超出边界[0 .. 96]'

我做错了什么?

dre*_*lax 16

NSRange的成员locationlength,不startend.这意味着您需要NSRange像这样创建结构:

NSMakeRange(startIndex, endIndex - startIndex);
Run Code Online (Sandbox Code Playgroud)