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 230019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:48:52+00:00 2026-05-11T19:48:52+00:00

NSArray has useful methods to find objects for specified indexes // To find objects

  • 0

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
- (NSUInteger)indexOfObject:(id)anObject

However, I want to get NSIndexSet (multiple indexes) for given objects. Something like:

- (NSIndexSet *)indexesOfObjects:(NSArray *)objects

This method does not exist for NSArray. Am I missing something? Does someone know another standard method? Otherwise I have to write this as a category method.

  • 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-11T19:48:52+00:00Added an answer on May 11, 2026 at 7:48 pm

    It might be useful to implement it using a set to specify the objects to find, such as:

    - (NSIndexSet *) indicesOfObjectsInSet: (NSSet *) set
    {
        if ( [set count] == 0 )
            return ( [NSIndexSet indexSet] );
    
        NSMutableIndexSet * indices = [NSMutableIndexSet indexSet];
    
        NSUInteger index = 0;
        for ( id obj in self )
        {
            if ( [set containsObject: obj] )
                [indices addIndex: index];
    
            index++;
        }
    
        return ( [[indices copy] autorelease] );
    }
    

    This requires visiting every object in the array, but at least only does so once and makes use of fast enumeration while doing so. Using an NSSet and testing each object in the array against that set is also much faster than testing for inclusion in an array.

    There’s a potential optimization here, but it would break in the case where a single object is stored in the receiving array multiple times:

    if ( [set containsObject: obj] )
    {
        [indices addIndex: index];
        if ( [indices count] == [set count] )
            break;
    }
    

    That way if you’re scanning a 20’000-item array for two objects and they’re both inside the first ten, you’ll be able to avoid scanning the other 19’990 objects in the array. As I said though, that doesn’t help if the array contains duplicates, because it’ll stop as soon as it’s found 2 indices (even if they both point to the same object).

    Having said that, I agree with Mike’s comment above. Chances are you’re setting yourself up for some pain come optimization-time. It may be worth thinking about different data types; for instance, while NSArray seems the most logical choice for a simple flat container, if you don’t actually need the ordering information it’s better to use an NSSet instead; this has the added advantage that it won’t store the same object (calculated using -isEqual:) twice. If you do want to keep track of duplicates, but don’t need ordering, you can use NSCountedSet, which behaves as NSSet except it keeps track of how many times each objects has been added/removed without actually storing duplicates.

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

Sidebar

Ask A Question

Stats

  • Questions 147k
  • Answers 147k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It looks like you're trying to replicate Entity Atribute Value… May 12, 2026 at 9:08 am
  • Editorial Team
    Editorial Team added an answer Have you looked into RabbitMQ? May 12, 2026 at 9:08 am
  • Editorial Team
    Editorial Team added an answer This is the correct code: add_action('wp_head', 'remove_widget_action', 1); function remove_widget_action()… May 12, 2026 at 9:08 am

Related Questions

My class has an NSArray that is filled with objects. In my dealloc method,
I'm creating a game that uses cards. I have an AppController class with one
This question has spawned out of this one. Working with lists of structs in
A question that has pondered me for the last while. I am primarily a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.