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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:37:29+00:00 2026-05-27T16:37:29+00:00

I have an NSArray containing n elements at indices 0, 1 … n-1 .

  • 0

I have an NSArray containing n elements at indices 0, 1 ... n-1. I want to populate an NSDictionary with the contents of my array.

Specifically the dictionary should contain key-value pairs where the key is the hash of the ith element in the array and the value is the index into the array.

For example: array = [123, 101, 199] then the dictionary will contain three key-value pairs:

  • ([123 hash], 0)
  • ([199 hash], 2)
  • ([101 hash], 1)

I’ve done this with a for loop over the array. What’s a more concise way to do this? Perhaps something from NSKeyValueCoding?

More info: I’m thinking of something like this:

NSArray *keys = [myArray valueForKey:@"hash"];
NSArray *values = [myArray valueForKey:@"index"]; // @"index" needs to change
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:values 
                                                       forKeys:keys];
  • 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-27T16:37:30+00:00Added an answer on May 27, 2026 at 4:37 pm

    I would probably use something like David’s for loop solution myself, but just for kicks, and because I’m still trying to wrap my head completely around them, I came up with a solution using blocks:

    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:[array count]];
    
    [array enumerateObjectsUsingBlock:^ (id obj, NSUInteger idx, BOOL *stop) {
        [dict setObject:[NSNumber numberWithUnsignedLong:idx] forKey:hashFor(obj)]; } ];
    

    I’m assuming the existence of a function hashFor that generates the hash. You can replace that part with a message to obj or whatever you do to generate the hash.

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

Sidebar

Related Questions

I have a NSArray containing objects. I want to create a secondary NSArray containing
I have an NSArray containing numbers as NSString objects. ie. [array addObject:[NSString stringWithFormat:@%d, 100]];
I have an array of dictionary items containing a list of menus and their
I have a plist containing an array with three elements all of which are
i have a NSArray containing some NSDictionaries which themselves also include a NSDictionary .
I have an NSArray of NSDictionary objects which I would like to be able
I have some NSDictionary objects stored in an NSArray called telephoneArray . I fetch
I have an NSArray storing NSDictionaries. Each dictionary has two keys; a name and
How can I get my object index? I have a dictionary containing arrays and
I have a 1D numpy array containing complex numbers (eg. numpy.complex64). I need to

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.