Questions tagged [nsindexset]

The NSIndexSet class represents an immutable collection of unique unsigned integers, known as indexes because of the way they are used. This collection is referred to as an index set. You use index sets in your code to store indexes into some other data structure. For example, given an NSArray object, you could use an index set to identify a subset of objects in that array.

NSIndexSet is a Foundation Framework collection class that is similar to NSRange, with the notable exception of being able to support non-contiguous series. An NSIndexSet can be created from a range using the indexSetWithIndexesInRange: class constructor:

NSIndexSet expresses a collection of unique whole numbers; its purpose is to express element numbers of an ordered collection, such as an NSArray. It is available in iOS 2.0 and later, available in OS X v10.3 and later . An NSIndexSet is immutable; its mutable subclass is NSMutableIndexSet. You can form a simple NSIndexSet consisting of just one contiguous range directly, by passing an NSRange to indexSetWithIndexesInRange: but to form a more complex index set you’ll need to use NSMutableIndexSet so that you can append additional ranges.

Example :

    NSRange range = NSMakeRange(0, 10);
    NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:range];

Related Source:

56 questions
63
votes
5 answers

Create NSIndexSet from integer array in Swift

I converted an NSIndexSet to an [Int] array using the answer at https://stackoverflow.com/a/28964059/6481734 I need to do essentially the opposite, turning the same kind of array back into an NSIndexSet.
Jacolack
  • 1,215
  • 2
  • 9
  • 23
47
votes
6 answers

Fast enumeration on an NSIndexSet

Can you fast enumerate a NSIndexSet? if not, what's the best way to enumerate the items in the set?
cfischer
  • 23,024
  • 36
  • 125
  • 209
32
votes
4 answers

How to use NSIndexSet

In Objective-C, my program opens a window and displays a table. I want to have a specified row of the table highlighted. How do I do this? I seem to need the code [myTableView selectRowIndexes:(NSIndexSet *) byExtendingSelection:(BOOL)]; I looked…
hertopnerd
  • 588
  • 1
  • 4
  • 9
17
votes
5 answers

How to get indexes from NSIndexset into an NSArray in cocoa?

I'm getting the select items from a table view with: NSIndexSet *selectedItems = [aTableView selectedRowIndexes]; what's the best way to get the indexes in a NSArray object?
Mr_Nizzle
  • 6,324
  • 11
  • 52
  • 85
15
votes
4 answers

NSIndexSet "-indexAtIndex:"?

This feels like a dumb question because it seems to me like my use case must be quite common. Say I want to represent a sparse set of indexes with an NSIndexSet (which is of course what it's for). I can use -firstIndex to get the lowest one and…
Ben Zotto
  • 67,174
  • 23
  • 136
  • 201
14
votes
3 answers

Create an NSRange with a given minimal and maximal value

I have a collection of int64_t values that I need to make an index set out of. I previously have used: NSIndexSet *set = [NSIndexSet indexSetWithRange:NSMakeRange(location, length)]; to make index sets, but this time its unclear to me how I would…
jac300
  • 4,832
  • 12
  • 49
  • 87
7
votes
3 answers

Get NSIndexSet from NSArray

NSArray has useful methods to find objects for specified indexes // To find objects by indexes - (id)objectAtIndex:(NSUInteger)index - (NSArray *)objectsAtIndexes:(NSIndexSet *)indexes // To find index by object -…
cocoafan
  • 4,794
  • 4
  • 35
  • 44
7
votes
1 answer

What's the difference between NSIndexSet and NSSet?

I'm a bit new to Cocoa and I was reading about NSIndexSet. Why is it necessary? It seems to me that NSIndexSet is nothing but a NSSet of integers, is that right? What's the purpose of creating a separate collection alltogether?
Tony
  • 30,345
  • 9
  • 45
  • 77
6
votes
2 answers

Is this a bug in NSIndexSet enumerateIndexesUsingBlock?

In my unit testing I was looking at some boundary conditions and my tests kept failing. I tracked it back to enumerating through an index set when its indexes extend all the way to NSNotFound-1 (the maximum legal value per the documentation). Using…
LavaSlider
  • 2,424
  • 17
  • 29
5
votes
1 answer

indexSetWithIndexesInRange is not doing what expected

I want to select some objects from an array. Therefore I'm using begin and end indexes of my selection. NSLog(@"start:%d\nend:%d", startIndex, endIndex); NSIndexSet *myIndexes = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(startIndex,…
testing
  • 17,950
  • 38
  • 208
  • 373
3
votes
1 answer

Objective-C: How to convert NSIndexSet to NSIndexPath

I would like to convert NSIndexSet Object to NSIndexPath,as effect as link: https://developer.apple.com/reference/photos/phphotolibrarychangeobserver?language=objc - (void)photoLibraryDidChange:(PHChange *)changeInfo { // Photos may call this…
JonphyChen
  • 119
  • 1
  • 11
3
votes
1 answer
3
votes
4 answers

Using enumerateObjectsAtIndexes or a for-loop to iterate through all indexes of an NSArray BEFORE a given index

What's the most concise way to iterate through the indexes of an NSArray that occur before a given index? For example: NSArray *myArray = @[ @"animal" , @"vegetable" , @"mineral" , @"piano" ]; [myArray enumerateObjectsAtIndexes:@"all before index…
zakdances
  • 16,715
  • 30
  • 94
  • 155
2
votes
3 answers

NSMutableArray keep objects at indexSet

I am on Xcode 8.2, OSX not iOS, Objective-C I have an NSMutableArray and an NSIndexSet. I want to remove ALL items of the array EXCEPT those at the NSIndexSet. So basically something like [array keepObjectsAtIndexes:indexSet]; // just to carify i…
Pat_Morita
  • 2,883
  • 3
  • 18
  • 30
2
votes
0 answers

How can an NSIndexSet be a root object?

Here is a typical code snippet when reordering a table with drag and drop. - (BOOL)tableView:(NSTableView *)tv writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard *)pboard { NSData *data = [NSKeyedArchiver…
Bob Ueland
  • 1,658
  • 14
  • 20
1
2 3 4