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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:16:11+00:00 2026-05-30T18:16:11+00:00

I need to create a structured NSDictionary with grouped keys, starting from a NSArray

  • 0

I need to create a structured NSDictionary with grouped keys, starting from a NSArray.
This is an example:

[
    {
        section = section1,
        category = category1,
        date = 2011-12-01,
        key1 = foo,
        key2 = bar
    },
    {
        section = section1,
        category = category2,
        date = 2011-12-01,
        key1 = foo,
        key2 = bar
    },
    {
        section = section1,
        category = category2,
        date = 2011-12-03
        key1 = foo,
        key2 = bar
    },
    {
        section = section2,
        category = category1,
        date = 2011-12-03
        key1 = foo,
        key2 = bar
    }
]  

The result should be a NSDictionary like this (I didn’t checked that the values are ok, I just want to give the idea):

[
    section1 = {
        category1 = {
            2011-12-01 = 
                [{
                    key1 = foo;
                    key2 = bar;
                },
                {
                    key1 = foo;
                    key2 = bar;
                }
                ]
            }
        },
        category2 = {
            2011-12-01 =
                [{
                    key1 = foo;
                    key2 = bar;
                }],
        }
    },
    section2 = {
        category1 = {
            2011-12-01 =
                [{
                    key1 = foo;
                    key2 = bar;
                }]
        }
    }
]

Can I achieve this using NSPredicate or Key-Value Coding, and avoid many loops?

My proposed solution

NSMutableDictionary *sectionEmpty = [NSMutableDictionary dictionary];
NSArray *values = [records allValues];

NSArray *sections = [[records allValues] valueForKeyPath:@"@distinctUnionOfObjects.section"];

for(NSString *section in sections) {
    // Find records for section
    NSArray *sectionRecords = [values filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"section == %@",section]];

    // Find unique categories
    NSArray *categorys = [sectionRecords valueForKeyPath:@"@distinctUnionOfObjects.category"];

    // Loop through categories
    for (NSString *category in categorys) {

        // Creating temporary record
        NSMutableDictionary *empty = [NSMutableDictionary dictionary];

        // Find records for category
        NSArray *categoryRecords = [sectionRecords filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"category == %@",category]];

        // Find unique dates
        NSArray *dates = [categoryRecords valueForKeyPath:@"@distinctUnionOfObjects.date"];

        // Loop through dates
        for (NSString *date in dates) {

            // Creating temporary record
            NSMutableDictionary *emptyDate = [NSMutableDictionary dictionary];

            // Find records for dates
            NSArray *dateRecords = [categoryRecords filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"date == %@",date]];

            // Split date
            NSString *dateString = [[date componentsSeparatedByString:@" "] objectAtIndex:0];

            // Check if date exist in temporary record
            if(![[emptyDate allKeys] containsObject:dateString]){
                [emptyDate setObject:[NSMutableArray array] forKey:dateString];
            }

            // Set date records for date key
            [[emptyDate objectForKey:dateString] addObject:dateRecords];

            // Set date for category
            [empty setObject:emptyDate forKey:category];
        }

        // Set category for section
        [sectionEmpty setObject:empty forKey:section];
    }

}
  • 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-30T18:16:12+00:00Added an answer on May 30, 2026 at 6:16 pm

    It seems the only way you can achieve what you want is a series of nested for loops (for each level: section, category, date). In the innermost loop you create a complex predicate like this:

    NSPredicate *predicate = [NSPredicate predicateWithFormat:
                              @"section = %@ AND category = %@ AND date = %@",
                              section, category, date];
    

    Then you filter out the input array, and construct new array of dictionaries with key1 and key2 only. You add this dates array to categories dictionary for a given date, and then you add a categories dictionary to the output dictionary for a given category. I don’t see any simpler way of doing this.

    NSArray *input = [records allValues];
    NSMutableDictionary *output = [NSMutableDictionary dictionary];
    
    
    NSArray *sections = [NSArray valueForKeyPath:@"@distinctUnionOfObjects.section"];
    NSArray *categories = [NSArray valueForKeyPath:@"@distinctUnionOfObjects.category"];
    NSArray *dates = [NSArray valueForKeyPath:@"@distinctUnionOfObjects.date"];
    
    NSPredicate *predicate;
    for (NSString *section in sections) {
        NSMutableDictionary *categoriesDictionary = [NSMutableDictionary dictionary];
    
        for (NSString *category in categories) {
            NSMutableArray *datesArray = [NSMutableArray array];
    
            for (NSString *date in dates) {
                predicate = [NSPredicate predicateWithFormat:
                             @"section = %@ AND category = %@ AND date = %@",
                             section, category, date];
    
                NSArray *filteredInput = [input filteredArrayUsingPredicate:predicate];
                if (filteredInput.count > 0) {
                    NSMutableArray *filteredOutput = [NSMutableArray arrayWithCapacity:filteredInput.count];
    
                    [filteredInput enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                        [filteredOutput addObject:
                         [NSDictionary dictionaryWithObjectsAndKeys:
                          [obj objectForKey:@"key1"], @"key1",
                          [obj objectForKey:@"key2"], @"key2", nil]
                         ];
                    }];
    
                    if (filteredOutput.count > 0)
                        [datesArray addObject:filteredOutput];
                }
            }
            if (datesArray.count > 0)
                [categoriesDictionary setObject:datesArray forKey:category];
        }
        if (categoriesDictionary.count > 0)
            [output setObject:categoriesDictionary forKey:section];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a historical timeline starting from 1600's to the present day.
I need to create a db structure where Supplier inherits from Client. I used
This follows on from my previous question about Moose structured types. I apologise for
I need to create a structure or series of strings that are fixed lenght
I need to create a structure that holds a variable number of 'char[2]'s, i.e.
I need to create an XML schema that validates a tree structure of an
In data processing, I frequently need to create a lookup data structure to map
Ok, so here's the entire structure I'm trying to create. I need to create
I need create custom dialog and put JPanel into it. Is it possible?
i need create an email list sending to many emails. what is best solution

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.