Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3667940
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:03:45+00:00 2026-05-19T02:03:45+00:00

This feels like a dumb question because it seems to me like my use

  • 0

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 -lastIndex for the highest, but what’s the canonical way to get a single, arbitrary index in the middle, given its “index”? The docs have left me unclear.

E.g. if I have an index set with the indexes { 0, 5, 8, 10, 12, 28 }, and I want to say “give me the fourth index” and I’d expect to get back 10 (or 12 I suppose depending on whether I count the zeroth, but let’s not get into that, you know what I mean).

Note that I’m not doing “enumeration” across the whole index set. At a given point in time I just want to know what the nth index in the set is by numerical order.

Maybe my data structure is wrong (“set”s aren’t usually designed for such ordered access), but there seems to be no NSIndexArray to speak of.

Am I missing something obvious?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T02:03:46+00:00Added an answer on May 19, 2026 at 2:03 am

    I believe NSIndexSet stores its indexes using ranges, so there isn’t necessarily a quick way to return the nth index. You could enumerate keeping a counter until your counter reaches your target index:

    NSUInteger index = [indexSet firstIndex];
    
    for (NSUInteger i = 0, target = 4; i < target; i++)
      index = [indexSet indexGreaterThanIndex:index];
    

    That should give you the 4th index. You could even add the method as a category method if you want:

    - (NSUInteger)indexAtIndex:(NSUInteger)anIndex
    {
        if (anIndex >= [self count])
          return NSNotFound;
    
        NSUInteger index = [indexSet firstIndex];
        for (NSUInteger i = 0; i < anIndex; i++)
          index = [self indexGreaterThanIndex:index];
        return index;
    }
    

    But, as you said, this may not be the best data structure to use so do consider that more before going with something like this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I feel like this is a stupid question because it seems like common sense
This feels like a dumb question, but I see the following in the Oracle
I feel like this is a dumb question and I'm missing something, but I
OK, this feels like an idiot question, but I'm stuck - I don't know
This feels like a really basic question, but I haven't been able to find
First let me say that I really feel directionless on this question. I am
I apologize if this is a dumb question, but hear me out: Dictionary<string, string>
This could potentially be a dumb question so apologies in advance if it is.
I feel like I should know this, but I haven't been able to figure
I'm sorry, but this is beginning to feel like kicking myself in the head.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.