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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:20:36+00:00 2026-06-15T14:20:36+00:00

I have NSDictionaries in NSArray just like below. array(dictionary(user:1, p1:1), dictionary(user:2, p1:3), dictionary(user:1, p1:5),

  • 0

I have NSDictionaries in NSArray just like below.

array(dictionary(“user”:1, “p1”:1), dictionary(“user”:2, “p1”:3),
dictionary(“user”:1, “p1”:5), dictionary(“user”:2, “p1”:7))

And I want to turn this array into dictionary like below.

NSArray *u1 = [NSArray arrayWithObjects:@"1", @"5", nil];
NSArray *u2 = [NSArray arrayWithObjects:@"3", @"7", nil];
keys = [NSArray arrayWithObjects:@"u1", @"u2", nil];
points = [NSDictionary dictionaryWithObjectsAndKeys:u1, @"u1", u2, @"u2", nil];

How can I do that? I am lost, can you guys please help me?

  • 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-15T14:20:37+00:00Added an answer on June 15, 2026 at 2:20 pm

    Another possible solution (works with an arbitrary number of users):

    NSArray *orig = @[
        @{@"user" : @"1", @"p1" : @"1"},
        @{@"user" : @"2", @"p1" : @"3"},
        @{@"user" : @"1", @"p1" : @"5"},
        @{@"user" : @"2", @"p1" : @"7"},
    ];
    
    // Create set of all users (without duplicates)
    NSSet *users = [NSSet setWithArray:[orig valueForKey:@"user"]];
    
    NSMutableDictionary *points = [NSMutableDictionary dictionary];
    for (NSString *user in users) {
        // newKey = "u" + username, e.g. "u1" or "u2":
        NSString *newKey = [@"u" stringByAppendingString:user];
    
        // newValue = array of "p1" values of the current user:
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"user == %@", user];
        NSArray *newValue = [[orig filteredArrayUsingPredicate:pred] valueForKey:@"p1"];
    
        // Add to dictionary:
        [points setObject:newValue forKey:newKey];
    }
    NSLog(@"%@", points);
    

    Output:

    {
        u1 =     (
            1,
            5
        );
        u2 =     (
            3,
            7
        );
    }
    

    And the keys can be obtained by

    NSArray *keys = [points allKeys];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some json coming like the below, which i'm trying to turn into
I have an NSArray with several NSDictionaries . I want to create a new
I have an NSArray storing NSDictionaries. Each dictionary has two keys; a name and
I have a few NSDictionaries inside of an NSArray. I want to modify a
I have an array which is filled with NSDictionaries . I want to find
I have an MSMutableArray that contains many NSDictionaries. A dictionary is just an NSString
I have an NSArray of NSDictionaries where each dictionary has the same keys. I
I have a problem with sorting... I need to sort NSArray containing NSDictionaries .
I have a bunch of NSDictionaries thats stored in an NSArray. The dictionaries store
I have an array of NSDictionaries. How can I pull out the first element

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.