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.

有帮助吗?

解决方案

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)

其他提示

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]];
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top