Create an NSRange with a given minimal and maximal value
Asked Answered
C

3

15

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.

Cordiecordier answered 17/10, 2013 at 15:1 Comment(2)
So the index set should contain the indices 1,3,4,6,7,9? Or did I misunderstand your question?Humbert
Yes, so I'm wondering how I would create the range 1-9 to get this to happen.Cordiecordier
H
23

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)
Humbert answered 17/10, 2013 at 15:13 Comment(4)
Sorry, I think I was unclear in my question. I just made an edit.Cordiecordier
Thanks - I think my question is much more basic than that - I have the max and min values already, I just don't see how to create a range with a min and max value.Cordiecordier
@user1697845: NSMakeRange(min, max + 1 - min) ?Humbert
Wow. I can't believe it was that simple. Yes that's all I needed thank you.Cordiecordier
D
0

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

Downpipe answered 17/10, 2013 at 15:13 Comment(0)
P
0

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]];
}
Pronation answered 17/10, 2013 at 15:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.