Question

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 do this based on the information I have.

Specifics: I have a collection of values. I know the highest value in the collection, the lowest value in the collection and the number of values in the collection. However, the values are not necessarily consecutive.

For example, if I have the following values: 1,3,4,6,7,9 I would like to create a set that includes all numbers in the collection between and including 1 and 9, i.e. set = 1-9.

How would I do this?

Edit

I shouldn't say I have a "collection" of integers, rather - I have objects stored in core data and each object has an int64_t attribute associated with it, so I don't have a pointer to an actual collection of the objects, and I want to avoid fetching the objects just to create a collection.

Was it helpful?

Solution

Edit: As it turned out in the discussion, the question was how to create a range with a given minimal and maximal value. This is done with

NSMakeRange(min, max + 1 - min)

OTHER TIPS

Just use a NSMutableIndexSet and add them one by one with the method addIndex

You could do something like this:

NSArray *array = @[@1,@3,@4,@6,@7,@9];

NSMutableIndexSet *set = [[NSMutableIndexSet alloc] init];
for (NSNumber *value in array) {
    [set addIndex:[value integerValue]];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top