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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:26:59+00:00 2026-06-16T23:26:59+00:00

I have an array of keys/values… NSArray *keysAndValues = @[@1, @one, @2, @two, @3,

  • 0

I have an array of keys/values…

NSArray *keysAndValues = @[@"1", @"one", @"2", @"two", @"3", @"three"];

What’s the simplest way to convert this to an NSDictionary so that it matches a dictionary that looks like…

NSDictionary *dict = @{@"1" : @"two", @"2" : @"two", @"3" : @"three"};

I have code that just cycles through the array and extracts the key/values, but I was wondering if there’s a cleaner way or any built in NSDictionary methods that I’m missing?

  • 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-06-16T23:27:00+00:00Added an answer on June 16, 2026 at 11:27 pm

    You’re not missing anything. There isn’t any really convenient way other than looping to do this. Of course any solution would be, at root, looping, but it would be nice if there were some more “filtering” or re-arranging abilities built in to NSArray (/me coughs discreetly and looks at Python list comprehensions).

    There’s likely to be something useful in this vein in vikingosegundo’s arraytools on GitHub, or similar collection-manipulation extensions by others. There’s certainly no harm in writing your own category method to do this, though, like I said, there would have to be a loop somewhere in there.

    Here’s my suggestion, for whatever that’s worth. Split the array by enumerating it:

    NSMutableArray * keys = [NSMutableArray array];
    NSMutableArray * values = [NSMutableArray array];
    [array enumerateObjectsUsingBlock:(void (^)(id obj, NSUInteger idx, BOOL *stop)){
        if( idx % 2 ){
            [values addObject:obj];
        else {
            [keys addObject:obj];
        }
    }];
    
    NSDictionary * d = [NSDictionary dictionaryWithObjects:values forKeys:keys];
    

    Seems like there should be a better way to do this, such as with an NSIndexSet and objectsAtIndexes:, but there’s no convenient way to create an index set with a non-contiguous bunch of indexes.

    Something like this, e.g., would be nice:

    @implementation NSIndexSet (WSSNonContiguous)
    
    
    + (id) WSS_indexSetWithOddIndexesInRange: (NSRange)range
    {
        NSMutableIndexSet * s = [NSMutableIndexSet indexSet];
    
        // If the start of the range is even, start with the next number.
        NSUInteger start = range.location % 2 ? range.location : range.location + 1;
        NSUInteger upper_limit = range.location + range.length;
        for( NSUInteger i = start; i < upper_limit; i += 2 ){
            [s addIndex:i];
        }
    
        return s;
    }
    
    + (id) WSS_indexSetWithEvenIndexesInRange: (NSRange)range
    {
        NSMutableIndexSet * s = [NSMutableIndexSet indexSet];
    
        // If the start of the range is odd, start with the next number.
        NSUInteger start = range.location % 2 ? range.location + 1 : range.location;
        NSUInteger upper_limit = range.location + range.length;
        for( NSUInteger i = start; i < upper_limit; i += 2 ){
            [s addIndex:i];
        }
    
        return s;
    }
    
    @end
    

    With a helper function:

    NSRange rangeOfNumbers(NSUInteger start, NSUInteger end)
    {
        return (NSRange){start, end-start};
    }
    

    So that you can do this:

    NSIndexSet * s = [NSIndexSet WSS_indexSetWithEvenIndexesInRange:rangeOfNumbers(0, 10)];
    NSLog(@"%@", s);
    // Prints: <NSMutableIndexSet: 0x7fccca4142a0>[number of indexes: 5 (in 5 ranges), indexes: (0 2 4 6 8)]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two arrays the first one has this structure Parameter Keys array array
I have built a custom hashmap using two arrays. One contains keys another values.
I have the following array of keys and values. How can i recreate this
i have a multidimensional array whose index/keys (not the values) are like this: this
Possible Duplicate: Merging PHP array, one as Keys the other as Values? i have
I have an array that has keys and values. For eg: Array ( [name]
I have a multidimensional array of arrays (source below), where my keys and values
I have a PHP 2D array, many keys, each with one value, I need
I have been having this issue in iterating through an array of keys and
I have two arrays: $array1 = array(1=>1,10=>1,12=>0,13=>13); $array2 = array(1=>Hello,10=>Test,12=>check,13=>error); Here $array1 has keys

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.